import tkinter as tk
import time
import math


class StereoClock:
    def __init__(self, root):
        # 初始化窗口
        self.root = root
        self.root.title("立体时钟")
        self.root.geometry("600x600")  # 窗口大小
        self.root.resizable(False, False)  # 固定窗口大小

        # 创建画布，用于绘制时钟
        self.canvas = tk.Canvas(root, width=600, height=600, bg="white")
        self.canvas.pack()

        # 时钟核心参数
        self.center_x = 300  # 时钟中心x坐标
        self.center_y = 300  # 时钟中心y坐标
        self.radius = 250    # 时钟半径

        # 启动时钟刷新
        self.update_clock()

    def draw_clock_frame(self):
        """绘制时钟的立体边框和刻度"""
        # 立体外框（双层圆形模拟厚度）
        # 底层阴影圆（偏下偏右）
        self.canvas.create_oval(
            self.center_x - self.radius - 5, self.center_y - self.radius - 5,
            self.center_x + self.radius + 5, self.center_y + self.radius + 5,
            outline="#cccccc", width=8
        )
        # 上层主体圆
        self.canvas.create_oval(
            self.center_x - self.radius, self.center_y - self.radius,
            self.center_x + self.radius, self.center_y + self.radius,
            outline="#333333", width=5
        )

        # 绘制刻度（分/秒刻度 + 小时刻度）
        for i in range(60):
            # 计算刻度坐标
            angle = math.radians(i * 6)  # 6度/刻度（360/60）
            # 外层刻度起点
            x1 = self.center_x + self.radius * math.sin(angle)
            y1 = self.center_y - self.radius * math.cos(angle)
            # 内层刻度终点（区分小时/分钟刻度）
            if i % 5 == 0:  # 小时刻度（更粗更长）
                x2 = self.center_x + (self.radius - 40) * math.sin(angle)
                y2 = self.center_y - (self.radius - 40) * math.cos(angle)
                self.canvas.create_line(
                    x1, y1, x2, y2, width=4, fill="#333333")
                # 绘制小时数字
                num = i // 5 if i // 5 != 0 else 12
                # 数字位置（比小时刻度更靠内）
                num_x = self.center_x + (self.radius - 60) * math.sin(angle)
                num_y = self.center_y - (self.radius - 60) * math.cos(angle)
                self.canvas.create_text(num_x, num_y, text=str(
                    num), font=("Arial", 20, "bold"))
            else:  # 分钟/秒钟刻度
                x2 = self.center_x + (self.radius - 20) * math.sin(angle)
                y2 = self.center_y - (self.radius - 20) * math.cos(angle)
                self.canvas.create_line(
                    x1, y1, x2, y2, width=2, fill="#666666")

    def draw_stereo_hand(self, angle, length, width, color, shadow_offset=2):
        """绘制立体指针（带阴影偏移）"""
        # 计算指针末端坐标
        x = self.center_x + length * math.sin(math.radians(angle))
        y = self.center_y - length * math.cos(math.radians(angle))

        # 先绘制阴影指针（偏下偏右，浅灰色），模拟立体阴影
        shadow_x = self.center_x + length * \
            math.sin(math.radians(angle)) + shadow_offset
        shadow_y = self.center_y - length * \
            math.cos(math.radians(angle)) + shadow_offset
        self.canvas.create_line(
            self.center_x, self.center_y, shadow_x, shadow_y,
            width=width, fill="#aaaaaa", capstyle=tk.ROUND
        )

        # 绘制主指针（白色描边增强立体）
        main_line = self.canvas.create_line(
            self.center_x, self.center_y, x, y,
            width=width, fill=color, capstyle=tk.ROUND
        )
        # 指针中心圆点（双层模拟立体）
        self.canvas.create_oval(
            self.center_x - 8, self.center_y - 8,
            self.center_x + 8, self.center_y + 8,
            fill="#aaaaaa", outline="white", width=2
        )
        self.canvas.create_oval(
            self.center_x - 6, self.center_y - 6,
            self.center_x + 6, self.center_y + 6,
            fill=color, outline="white", width=1
        )
        return main_line

    def update_clock(self):
        """实时更新时钟"""
        # 清空画布（保留背景）
        self.canvas.delete("all")

        # 绘制时钟框架（边框、刻度、数字）
        self.draw_clock_frame()

        # 获取当前时间
        current_time = time.localtime()
        hour = current_time.tm_hour % 12  # 转为12小时制
        minute = current_time.tm_min
        second = current_time.tm_sec

        # 计算各指针角度（钟表0点在上方，角度从0开始顺时针）
        # 时针：30度/小时 + 0.5度/分钟（360/12=30，30/60=0.5）
        hour_angle = hour * 30 + minute * 0.5
        # 分针：6度/分钟（360/60=6）
        minute_angle = minute * 6 + second * 0.1
        # 秒针：6度/秒
        second_angle = second * 6

        # 绘制立体指针（不同长度/粗细/颜色，增强层次）
        self.draw_stereo_hand(hour_angle, self.radius *
                              0.5, 8, "#222222")    # 时针（短粗）
        self.draw_stereo_hand(minute_angle, self.radius *
                              0.7, 6, "#444444")  # 分针（中长）
        self.draw_stereo_hand(second_angle, self.radius *
                              0.85, 3, "#ff4444")  # 秒针（细长红色）

        # 每秒刷新一次
        self.root.after(1000, self.update_clock)


if __name__ == "__main__":
    # 创建主窗口并启动时钟
    root = tk.Tk()
    clock = StereoClock(root)
    root.mainloop()
