import tkinter as tk
from tkinter import font
from time import strftime, localtime
import sys

class DigitalClock:
    def __init__(self, root):
        self.root = root
        self.root.title("Python 数字时钟")
        self.root.geometry("600x300")
        self.root.resizable(False, False)
        self.root.configure(bg="#2b2b2b")  # 深灰色背景

        # 状态变量
        self.is_24_hour = False  # 默认为12小时制
        self.is_fullscreen = False

        # 设置字体
        # 尝试使用系统自带的等宽或清晰字体
        self.font_time = font.Font(family="Helvetica", size=80, weight="bold")
        self.font_date = font.Font(family="Arial", size=20)
        self.font_mode = font.Font(family="Arial", size=12)

        # --- 界面布局 ---
        
        # 时间标签
        self.lbl_time = tk.Label(
            root, 
            text="", 
            font=self.font_time, 
            bg="#2b2b2b", 
            fg="#00ffcc"  # 青色数字
        )
        self.lbl_time.pack(pady=(40, 0))

        # 日期标签
        self.lbl_date = tk.Label(
            root, 
            text="", 
            font=self.font_date, 
            bg="#2b2b2b", 
            fg="#aaaaaa"  # 灰色文字
        )
        self.lbl_date.pack()

        # 底部控制栏
        self.frame_controls = tk.Frame(root, bg="#2b2b2b")
        self.frame_controls.pack(side=tk.BOTTOM, pady=20)

        # 模式切换按钮
        self.btn_mode = tk.Button(
            self.frame_controls,
            text="切换 24h/12h",
            command=self.toggle_format,
            bg="#444444",
            fg="white",
            activebackground="#666666",
            activeforeground="white",
            border=0,
            padx=15,
            pady=5,
            cursor="hand2"
        )
        self.btn_mode.pack(side=tk.LEFT, padx=10)

        # 退出按钮
        self.btn_exit = tk.Button(
            self.frame_controls,
            text="退出",
            command=root.quit,
            bg="#444444",
            fg="white",
            activebackground="#d9534f",
            activeforeground="white",
            border=0,
            padx=15,
            pady=5,
            cursor="hand2"
        )
        self.btn_exit.pack(side=tk.RIGHT, padx=10)

        # 提示标签
        self.lbl_hint = tk.Label(
            self.frame_controls,
            text="按 F11 全屏",
            font=self.font_mode,
            bg="#2b2b2b",
            fg="#666666"
        )
        self.lbl_hint.pack(side=tk.BOTTOM, pady=(10, 0))

        # 绑定键盘事件
        self.root.bind("<F11>", self.toggle_fullscreen)
        self.root.bind("<Escape>", self.exit_fullscreen)

        # 启动时钟更新
        self.update_clock()

    def update_clock(self):
        """获取当前时间并更新界面"""
        if self.is_24_hour:
            time_string = strftime("%H:%M:%S")
        else:
            time_string = strftime("%I:%M:%S %p")
        
        date_string = strftime("%Y年%m月%d日 %A") # %A 是星期几
        
        self.lbl_time.config(text=time_string)
        self.lbl_date.config(text=date_string)
        
        # 每 1000 毫秒 (1秒) 调用一次自己
        self.lbl_time.after(1000, self.update_clock)

    def toggle_format(self):
        """切换 12/24 小时制"""
        self.is_24_hour = not self.is_24_hour
        # 立即更新一次，不用等下一秒
        if self.is_24_hour:
            self.btn_mode.config(text="切换回 12h")
        else:
            self.btn_mode.config(text="切换 24h")
        self.update_clock()

    def toggle_fullscreen(self, event=None):
        """切换全屏模式"""
        self.is_fullscreen = not self.is_fullscreen
        self.root.attributes("-fullscreen", self.is_fullscreen)
        
        if self.is_fullscreen:
            # 全屏时调整字体大小以适应屏幕
            w = self.root.winfo_screenwidth()
            scale = w / 1000  # 假设基准宽度为1000
            new_size = int(80 * scale)
            self.font_time.config(size=max(40, new_size))
            self.lbl_hint.pack_forget() # 全屏隐藏提示
        else:
            # 恢复窗口大小时还原字体
            self.font_time.config(size=80)
            self.lbl_hint.pack(side=tk.BOTTOM, pady=(10, 0))
            self.root.geometry("600x300")

    def exit_fullscreen(self, event=None):
        """按 ESC 退出全屏"""
        if self.is_fullscreen:
            self.toggle_fullscreen()

if __name__ == "__main__":
    root = tk.Tk()
    
    # 尝试让窗口居中
    root.update_idletasks()
    width = 600
    height = 300
    x = (root.winfo_screenwidth() // 2) - (width // 2)
    y = (root.winfo_screenheight() // 2) - (height // 2)
    root.geometry(f'{width}x{height}+{x}+{y}')

    app = DigitalClock(root)
    root.mainloop()