import tkinter as tk
import math
from datetime import datetime

class ClockApp:
    def __init__(self, root):
        self.root = root
        self.root.title("010. 时钟")
        self.root.resizable(False, False)

        # ===== 主容器 =====
        main = tk.Frame(root, bg="#1a1a2e")
        main.pack()

        # ===== 画布：模拟表盘 =====
        self.canvas = tk.Canvas(
            main, width=400, height=400,
            bg="#16213e", highlightthickness=0
        )
        self.canvas.pack(padx=20, pady=(20, 10))

        # ===== 数字时间显示 =====
        self.time_label = tk.Label(
            main, text="", font=("Consolas", 28, "bold"),
            fg="#00d4ff", bg="#1a1a2e"
        )
        self.time_label.pack(pady=5)

        # ===== 日期显示 =====
        self.date_label = tk.Label(
            main, text="", font=("微软雅黑", 12),
            fg="#aaaaaa", bg="#1a1a2e"
        )
        self.date_label.pack(pady=(0, 20))

        # 表盘参数
        self.cx = 200
        self.cy = 200
        self.r = 170

        self.draw_dial()
        self.update_clock()

    # ===== 绘制表盘 =====
    def draw_dial(self):
        # 外圈
        self.canvas.create_oval(
            self.cx - self.r, self.cy - self.r,
            self.cx + self.r, self.cy + self.r,
            outline="#00d4ff", width=4
        )

        # 内圈装饰
        self.canvas.create_oval(
            self.cx - self.r + 8, self.cy - self.r + 8,
            self.cx + self.r - 8, self.cy + self.r - 8,
            outline="#333", width=1
        )

        # 刻度 + 数字
        for i in range(60):
            angle = math.radians(i * 6 - 90)

            if i % 5 == 0:
                # 小时刻度（长）
                x1 = self.cx + (self.r - 12) * math.cos(angle)
                y1 = self.cy + (self.r - 12) * math.sin(angle)
                x2 = self.cx + self.r * math.cos(angle)
                y2 = self.cy + self.r * math.sin(angle)
                self.canvas.create_line(x1, y1, x2, y2, fill="#00d4ff", width=3)

                # 小时数字
                hour = i // 5 if i // 5 != 0 else 12
                nx = self.cx + (self.r - 30) * math.cos(angle)
                ny = self.cy + (self.r - 30) * math.sin(angle)
                self.canvas.create_text(
                    nx, ny, text=str(hour),
                    fill="#00d4ff", font=("Arial", 14, "bold")
                )
            else:
                # 分钟刻度（短）
                x1 = self.cx + (self.r - 6) * math.cos(angle)
                y1 = self.cy + (self.r - 6) * math.sin(angle)
                x2 = self.cx + self.r * math.cos(angle)
                y2 = self.cy + self.r * math.sin(angle)
                self.canvas.create_line(x1, y1, x2, y2, fill="#555", width=1)

        # 中心点
        self.canvas.create_oval(
            self.cx - 6, self.cy - 6,
            self.cx + 6, self.cy + 6,
            fill="#00d4ff", outline="#00d4ff"
        )

    # ===== 绘制指针 =====
    def draw_hands(self, hour, minute, second):
        # 先清掉旧的指针
        self.canvas.delete("hand")

        sec_angle = math.radians(second * 6 - 90)
        min_angle = math.radians(minute * 6 + second * 0.1 - 90)
        hr_angle = math.radians((hour % 12) * 30 + minute * 0.5 - 90)

        # 秒针（红色，最长）
        self._draw_hand(sec_angle, self.r - 20, "#ff4444", 2, "hand")

        # 分针（白色，中等）
        self._draw_hand(min_angle, self.r - 40, "#ffffff", 4, "hand")

        # 时针（蓝色，最短）
        self._draw_hand(hr_angle, self.r - 65, "#00d4ff", 6, "hand")

        # 中心圆点（覆盖指针根部）
        self.canvas.create_oval(
            self.cx - 8, self.cy - 8,
            self.cx + 8, self.cy + 8,
            fill="#00d4ff", outline="", tags="hand"
        )

    def _draw_hand(self, angle, length, color, width, tag):
        x = self.cx + length * math.cos(angle)
        y = self.cy + length * math.sin(angle)
        self.canvas.create_line(
            self.cx, self.cy, x, y,
            fill=color, width=width, capstyle=tk.ROUND, tags=tag
        )

    # ===== 更新时钟 =====
    def update_clock(self):
        now = datetime.now()
        hour = now.hour
        minute = now.minute
        second = now.second
        microsecond = now.microsecond

        # 更精确的秒（含毫秒）
        exact_sec = second + microsecond / 1_000_000

        # 绘制指针
        self.draw_hands(hour, minute, exact_sec)

        # 数字时间
        time_str = now.strftime("%H:%M:%S")
        self.time_label.config(text=time_str)

        # 日期
        weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
        weekday = weekdays[now.weekday()]
        date_str = now.strftime(f"%Y年%m月%d日  {weekday}")
        self.date_label.config(text=date_str)

        # 约60fps刷新
        self.root.after(16, self.update_clock)


if __name__ == "__main__":
    root = tk.Tk()
    app = ClockApp(root)
    root.mainloop()