import tkinter as tk
from datetime import datetime
import math


class ClockApp:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("多功能时钟")
        self.root.geometry("500x600")
        self.root.resizable(False, False)

        # 设置窗口居中
        self.center_window()

        # 创建顶部按钮框架
        button_frame = tk.Frame(self.root)
        button_frame.pack(pady=10)

        self.digital_btn = tk.Button(button_frame, text="数字时钟",
                                     command=self.show_digital, width=12)
        self.analog_btn = tk.Button(button_frame, text="模拟时钟",
                                    command=self.show_analog, width=12)

        self.digital_btn.pack(side=tk.LEFT, padx=5)
        self.analog_btn.pack(side=tk.LEFT, padx=5)

        # 创建主显示区域
        self.main_frame = tk.Frame(self.root)
        self.main_frame.pack(expand=True, fill='both', padx=10, pady=10)

        # 默认显示数字时钟
        self.current_mode = "digital"
        self.show_digital()

        # 存储定时器ID用于清理
        self.timer_id = None

    def center_window(self):
        """将窗口居中显示"""
        self.root.update_idletasks()
        width = self.root.winfo_width()
        height = self.root.winfo_height()
        x = (self.root.winfo_screenwidth() // 2) - (width // 2)
        y = (self.root.winfo_screenheight() // 2) - (height // 2)
        self.root.geometry(f'{width}x{height}+{x}+{y}')

    def clear_main_frame(self):
        """清空主显示区域"""
        for widget in self.main_frame.winfo_children():
            widget.destroy()
        if self.timer_id:
            self.root.after_cancel(self.timer_id)
            self.timer_id = None

    def show_digital(self):
        """显示数字时钟"""
        if self.current_mode == "digital":
            return

        self.clear_main_frame()
        self.current_mode = "digital"

        # 时间显示
        self.time_label = tk.Label(
            self.main_frame,
            font=('Arial', 48, 'bold'),
            bg='black',
            fg='lime',
            relief='sunken',
            bd=10
        )
        self.time_label.pack(expand=True, fill='both', padx=20, pady=20)

        # 日期显示
        self.date_label = tk.Label(
            self.main_frame,
            font=('Arial', 16),
            bg='black',
            fg='white'
        )
        self.date_label.pack(pady=5)

        self.update_digital_clock()

    def show_analog(self):
        """显示模拟时钟"""
        if self.current_mode == "analog":
            return

        self.clear_main_frame()
        self.current_mode = "analog"

        self.canvas = tk.Canvas(
            self.main_frame, width=400, height=400, bg='white')
        self.canvas.pack(expand=True)

        self.center_x = 200
        self.center_y = 200
        self.radius = 180

        self.draw_clock_face()
        self.update_analog_clock()

    def update_digital_clock(self):
        """更新数字时钟"""
        current_time = datetime.now().strftime("%H:%M:%S")
        current_date = datetime.now().strftime("%Y年%m月%d日 %A")

        weekday_map = {
            'Monday': '星期一', 'Tuesday': '星期二', 'Wednesday': '星期三',
            'Thursday': '星期四', 'Friday': '星期五', 'Saturday': '星期六', 'Sunday': '星期日'
        }
        current_date = current_date.split()
        current_date[-1] = weekday_map.get(current_date[-1], current_date[-1])
        current_date = ' '.join(current_date)

        self.time_label.config(text=current_time)
        self.date_label.config(text=current_date)

        self.timer_id = self.root.after(1000, self.update_digital_clock)

    def draw_clock_face(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='black', width=3
        )

        self.canvas.create_oval(
            self.center_x - 8, self.center_y - 8,
            self.center_x + 8, self.center_y + 8,
            fill='red', outline='black'
        )

        for i in range(12):
            angle = math.radians(i * 30 - 90)
            x_outer = self.center_x + (self.radius - 10) * math.cos(angle)
            y_outer = self.center_y + (self.radius - 10) * math.sin(angle)
            x_inner = self.center_x + (self.radius - 20) * math.cos(angle)
            y_inner = self.center_y + (self.radius - 20) * math.sin(angle)
            self.canvas.create_line(
                x_inner, y_inner, x_outer, y_outer, width=3)

            x_text = self.center_x + (self.radius - 40) * math.cos(angle)
            y_text = self.center_y + (self.radius - 40) * math.sin(angle)
            self.canvas.create_text(x_text, y_text, text=str(i if i != 0 else 12),
                                    font=('Arial', 16, 'bold'))

    def draw_hand(self, angle, length, width, color):
        """绘制指针"""
        x_end = self.center_x + length * math.cos(angle)
        y_end = self.center_y + length * math.sin(angle)
        return self.canvas.create_line(self.center_x, self.center_y, x_end, y_end,
                                       width=width, fill=color, arrow='last')

    def update_analog_clock(self):
        """更新模拟时钟"""
        self.canvas.delete("hands")

        now = datetime.now()
        hour = now.hour % 12
        minute = now.minute
        second = now.second

        hour_angle = math.radians((hour * 30) + (minute * 0.5) - 90)
        minute_angle = math.radians((minute * 6) + (second * 0.1) - 90)
        second_angle = math.radians(second * 6 - 90)

        self.draw_hand(hour_angle, self.radius * 0.5, 6, 'black')
        self.draw_hand(minute_angle, self.radius * 0.7, 4, 'blue')
        self.draw_hand(second_angle, self.radius * 0.8, 2, 'red')

        digital_time = now.strftime("%H:%M:%S")
        self.canvas.create_text(self.center_x, self.center_y + self.radius + 20,
                                text=digital_time, font=('Arial', 16), tags="hands")

        self.timer_id = self.root.after(1000, self.update_analog_clock)

    def run(self):
        """运行应用"""
        self.root.mainloop()


# 运行综合时钟
if __name__ == "__main__":
    app = ClockApp()
    app.run()
