import tkinter as tk
from datetime import datetime
import calendar
import time

class DigitalClock:
    def __init__(self, root):
        self.root = root
        self.root.title("Tkinter数字时钟")
        self.root.geometry("500x350")
        self.root.configure(bg='#2C3E50')
        self.root.resizable(False, False)
        
        # 设置窗口图标
        try:
            self.root.iconbitmap(default='clock.ico')
        except:
            pass
        
        # 主容器
        self.main_frame = tk.Frame(root, bg='#2C3E50')
        self.main_frame.pack(expand=True, fill='both', padx=20, pady=20)
        
        # 时间显示标签
        self.time_label = tk.Label(
            self.main_frame,
            font=('Arial', 60, 'bold'),
            bg='#2C3E50',
            fg='#ECF0F1'
        )
        self.time_label.pack(pady=(20, 10))
        
        # 日期显示标签
        self.date_label = tk.Label(
            self.main_frame,
            font=('Arial', 24),
            bg='#2C3E50',
            fg='#BDC3C7'
        )
        self.date_label.pack(pady=10)
        
        # 星期显示标签
        self.day_label = tk.Label(
            self.main_frame,
            font=('Arial', 20),
            bg='#2C3E50',
            fg='#3498DB'
        )
        self.day_label.pack(pady=5)
        
        # 农历信息标签（示例，实际需要农历库）
        self.lunar_label = tk.Label(
            self.main_frame,
            font=('Microsoft YaHei', 16),
            bg='#2C3E50',
            fg='#E74C3C'
        )
        self.lunar_label.pack(pady=10)
        
        # 控制按钮框架
        self.button_frame = tk.Frame(self.main_frame, bg='#2C3E50')
        self.button_frame.pack(pady=20)
        
        # 24小时制切换按钮
        self.is_24h = tk.BooleanVar(value=True)
        self.format_button = tk.Checkbutton(
            self.button_frame,
            text="12小时制",
            variable=self.is_24h,
            command=self.toggle_format,
            font=('Arial', 12),
            bg='#2C3E50',
            fg='#ECF0F1',
            selectcolor='#34495E',
            activebackground='#2C3E50',
            activeforeground='#ECF0F1'
        )
        self.format_button.pack(side='left', padx=10)
        
        # 退出按钮
        self.quit_button = tk.Button(
            self.button_frame,
            text="退出",
            command=root.quit,
            font=('Arial', 12, 'bold'),
            bg='#E74C3C',
            fg='white',
            padx=20,
            pady=5,
            bd=0,
            activebackground='#C0392B',
            activeforeground='white'
        )
        self.quit_button.pack(side='left', padx=10)
        
        # 更新时间
        self.update_time()
        
    def update_time(self):
        """更新时间显示"""
        now = datetime.now()
        
        # 格式化时间
        if self.is_24h.get():
            current_time = now.strftime("%H:%M:%S")
        else:
            current_time = now.strftime("%I:%M:%S %p")
        
        # 格式化日期
        current_date = now.strftime("%Y年%m月%d日")
        
        # 获取星期
        weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
        weekday = weekdays[now.weekday()]
        
        # 更新标签
        self.time_label.config(text=current_time)
        self.date_label.config(text=current_date)
        self.day_label.config(text=weekday)
        
        # 农历信息（这里只是示例，实际需要农历计算库）
        lunar_info = self.get_lunar_info(now)
        self.lunar_label.config(text=lunar_info)
        
        # 每秒更新一次
        self.root.after(1000, self.update_time)
    
    def toggle_format(self):
        """切换12/24小时制"""
        # 按钮文本会根据状态变化
        if self.is_24h.get():
            self.format_button.config(text="12小时制")
        else:
            self.format_button.config(text="24小时制")
    
    def get_lunar_info(self, dt):
        """获取农历信息（简化版，实际应用中需要农历库）"""
        year = dt.year
        month = dt.month
        day = dt.day
        
        # 农历年份对应（简化处理）
        heavenly_stems = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
        earthly_branches = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
        zodiac = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
        
        # 计算农历年份（简化算法，不精确）
        # 注意：这是2026年，丙午马年
        lunar_year_index = (year - 4) % 60
        stem_index = lunar_year_index % 10
        branch_index = lunar_year_index % 12
        
        heavenly_stem = heavenly_stems[stem_index]
        earthly_branch = earthly_branches[branch_index]
        zodiac_sign = zodiac[branch_index]
        
        return f"农历 {heavenly_stem}{earthly_branch}年 ({zodiac_sign}年)"

def main():
    root = tk.Tk()
    clock = DigitalClock(root)
    root.mainloop()

if __name__ == "__main__":
    main()