import tkinter as tk
from tkinter import font
import math
import time

# 十二时辰对应表
SHICHEN = [
    "子", "丑", "寅", "卯", "辰", "巳",
    "午", "未", "申", "酉", "戌", "亥"
]
# 时辰对应小时
SHICHEN_HOUR = [23, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]

class AncientClock:
    def __init__(self, root):
        self.root = root
        self.root.title("时钟")
        self.root.geometry("600x650")
        # 古风底色 宣纸米黄色
        self.root.config(bg="#f8f2e4")
        
        # 画布
        self.canvas = tk.Canvas(root, width=600, height=600, bg="#f8f2e4", highlightthickness=0)
        self.canvas.pack()
        
        # 底部文字显示当前时辰、公历时间
        self.info_label = tk.Label(
            root, text="", font=("楷体", 16), bg="#f8f2e4", fg="#4a2c2a"
        )
        self.info_label.pack(pady=10)
        
        self.center_x = 300
        self.center_y = 300
        self.radius = 260
        self.update_clock()

    def draw_clock_bg(self):
        # 画外圈古铜色圆环
        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="#8c5a38", width=6
        )
        # 内层细圈
        self.canvas.create_oval(
            self.center_x - self.radius + 15, self.center_y - self.radius + 15,
            self.center_x + self.radius - 15, self.center_y + self.radius - 15,
            outline="#b88c64", width=2
        )
        
        # 绘制十二时辰刻度与文字
        for i in range(12):
            angle = math.radians(i * 30 - 90)
            # 刻度短线
            outer_x = self.center_x + (self.radius - 10) * math.cos(angle)
            outer_y = self.center_y + (self.radius - 10) * math.sin(angle)
            inner_x = self.center_x + (self.radius - 30) * math.cos(angle)
            inner_y = self.center_y + (self.radius - 30) * math.sin(angle)
            self.canvas.create_line(outer_x, outer_y, inner_x, inner_y, fill="#5c3d2e", width=3)
            
            # 时辰文字
            text_x = self.center_x + (self.radius - 55) * math.cos(angle)
            text_y = self.center_y + (self.radius - 55) * math.sin(angle)
            self.canvas.create_text(
                text_x, text_y, text=SHICHEN[i], font=("楷体", 24), fill="#3a2413"
            )
        
        # 中心圆点
        self.canvas.create_oval(
            self.center_x - 10, self.center_y - 10,
            self.center_x + 10, self.center_y + 10,
            fill="#5c2c20"
        )

    def draw_hand(self, length, angle, color, width):
        rad = math.radians(angle - 90)
        end_x = self.center_x + length * math.cos(rad)
        end_y = self.center_y + length * math.sin(rad)
        self.canvas.create_line(
            self.center_x, self.center_y, end_x, end_y,
            fill=color, width=width, capstyle="round"
        )

    def get_shichen(self, hour):
        """根据小时换算十二时辰"""
        if hour == 0:
            hour = 24
        for idx, h in enumerate(SHICHEN_HOUR):
            if h <= hour < h + 2:
                return SHICHEN[idx]
        return "子"

    def update_clock(self):
        self.canvas.delete("all")
        self.draw_clock_bg()
        
        # 获取当前时间
        now = time.localtime()
        h = now.tm_hour
        m = now.tm_min
        s = now.tm_sec
        
        # 计算指针角度
        sec_angle = s * 6
        min_angle = m * 6 + s * 0.1
        hour_angle = (h % 12) * 30 + m * 0.5
        
        # 绘制指针
        self.draw_hand(self.radius - 80, hour_angle, "#3a2010", 7)  # 时辰时针
        self.draw_hand(self.radius - 50, min_angle, "#6b3f28", 4)  # 分针
        self.draw_hand(self.radius - 30, sec_angle, "#992828", 2)  # 秒针
        
        # 更新底部文字信息
        shi_chen = self.get_shichen(h)
        time_str = f"当前时辰：{shi_chen}时 | 现代时间：{h:02d}:{m:02d}:{s:02d}"
        self.info_label.config(text=time_str)
        
        # 循环刷新
        self.root.after(100, self.update_clock)

if __name__ == "__main__":
    window = tk.Tk()
    app = AncientClock(window)
    window.mainloop()