import tkinter as tk
from tkinter import ttk, colorchooser
import time
import json
import os
import pystray
from PIL import Image, ImageDraw
class ClockApp(tk.Toplevel):
def init(self, master=None):
super().init(master)
self.master = master
self.config = self.load_config()
# 设置窗口属性
self.overrideredirect(True)
self.wm_attributes("-transparentcolor", "gray99")
self.wm_attributes("-topmost", self.config.get("always_on_top", True))
# 绑定关闭事件
self.bind('<Escape>', self.close)
# 获取屏幕尺寸
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
# 创建时间显示框架
self.timeframe = tk.Frame(self, width=screen_width, height=screen_height, bg="gray99")
self.timeframe.pack(expand=True, fill='both')
# 时间标签
self.time_var = tk.StringVar()
self.time_label = tk.Label(self.timeframe, textvariable=self.time_var, fg=self.config.get("text_color", "black"), bg="gray99", font=("NovaMono", 60))
self.time_label.place(y=screen_height/2-70, x=screen_width/2, anchor="center")
# 日期标签
self.date_var = tk.StringVar()
self.date_label = tk.Label(self.timeframe, textvariable=self.date_var, fg=self.config.get("text_color", "black"), bg="gray99", font=("Bahnschrift", 25))
self.date_label.place(y=screen_height/2+80, x=screen_width/2, anchor="center")
# 更新自定义文本
self.zidingyi_var = tk.StringVar()
self.zidingyi_label = tk.Label(self.timeframe, textvariable=self.zidingyi_var,
fg=self.config.get("text_color", "black"), bg="gray99", font=("Bahnschrift", 25))
self.zidingyi_label.place(y=screen_height / 2 + 40, x=screen_width / 2, anchor="center")
self.zidingyi_var.set(value=self.config.get("custom_text", "")) # 设置初始文本
# 启动更新时间线程
self.running = True
self.update_clock()
# 创建托盘图标
self.create_tray_icon()
def create_tray_icon(self):
# 创建一个白色背景的图像
image = Image.new('RGBA', (64, 64), (255, 255, 255, 0))
draw = ImageDraw.Draw(image)
draw.rectangle((8, 8, 56, 56), fill=(255, 255, 255, 255))
draw.ellipse((20, 20, 44, 44), fill=(0, 0, 0, 255))
menu = (
pystray.MenuItem('Configure', self.open_config_window),
pystray.MenuItem('Exit', self.close)
)
self.tray_icon = pystray.Icon("name", image, "Clock App", menu)
self.tray_icon.run_detached()
def update_clock(self):
if self.running:
print("Updating clock")
self.time_var.set(value=time.strftime("%H:%M:%S"))
self.date_var.set(value=time.strftime("%A, %e %B"))
self.zidingyi_var.set(value=self.config.get("custom_text", ""))
self._timer_id = self.after(1000, self.update_clock) # 保存返回的标识符
def close(self, event=None):
print("ClockApp closed")
self.running = False
if hasattr(self, '_timer_id'):
self.after_cancel(self._timer_id) # 取消挂起的 after 调用
self.tray_icon.stop() # 先停止托盘图标
self.destroy() # 然后销毁窗口
def open_config_window(self):
ConfigWindow(self)
def load_config(self):
config_path = 'config.json'
default_config = {
"text_color": "black",
"always_on_top": True,
"custom_text": "吾爱破解,阿离牙多"
}
if os.path.exists(config_path):
with open(config_path, 'r') as file:
return json.load(file)
else:
with open(config_path, 'w') as file:
json.dump(default_config, file)
return default_config
def save_config(self, config):
with open('config.json', 'w') as file:
json.dump(config, file)
配置窗口类
class ConfigWindow(tk.Toplevel):
def init(self, parent):
super().init(parent)
self.parent = parent
self.title("吾爱破解aliyaduo")
# 添加 Text 控件
self.custom_text = tk.Text(self, height=1, width=30)
self.custom_text.insert(tk.END, self.parent.config["custom_text"])
self.custom_text.grid(row=2, columnspan=2, sticky='ew')
# 颜色选择
self.color_button = tk.Button(self, text="Choose Color", command=self.choose_color)
self.color_button.grid(row=0, column=0, sticky='ew')
self.color_preview = tk.Label(self, bg=self.parent.config["text_color"])
self.color_preview.grid(row=0, column=1, sticky='ew')
# 置顶选项
self.always_on_top_var = tk.BooleanVar(value=self.parent.config["always_on_top"])
self.always_on_top_checkbutton = tk.Checkbutton(self, text="Always on Top", variable=self.always_on_top_var)
self.always_on_top_checkbutton.grid(row=1, columnspan=2, sticky='ew')
# 应用按钮
self.apply_button = tk.Button(self, text="Apply", command=self.apply_changes)
self.apply_button.grid(row=3, columnspan=2, sticky='ew')
def choose_color(self):
color_code = colorchooser.askcolor(title="Choose color")
if color_code[1]:
self.color_preview.config(bg=color_code[1])
def apply_changes(self):
new_config = {
"text_color": self.color_preview.cget("bg"),
"always_on_top": self.always_on_top_var.get(),
"custom_text": self.custom_text.get("1.0", tk.END).strip()
}
self.parent.config = new_config
self.parent.save_config(new_config)
self.parent.zidingyi_var.set(value=new_config["custom_text"]) # 更新自定义文本
self.parent.wm_attributes("-topmost", new_config["always_on_top"])
self.parent.time_label.config(fg=new_config["text_color"])
self.parent.date_label.config(fg=new_config["text_color"])
self.parent.zidingyi_label.config(fg=new_config["text_color"])
self.destroy()
if name == "main":
root = tk.Tk()
root.withdraw() # 隐藏主窗口
app = ClockApp(master=root)
app.mainloop()