吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1334|回复: 47
收起左侧

[原创工具] 简单自定义桌面时钟

[复制链接]
aliyaduo 发表于 2024-7-23 11:49
用python tk 做了个常驻桌面的时钟, 自己平时主攻pySide6, 对tk 不是很熟, 拿来练练手.
功能其实很简陋, 就是在桌面正中央放一个大的时钟, 能看时间, 中间写一句彩虹屁夸夸自己, 下边显示日期, 允许用户通过托盘右键自定义文字, 时钟颜色, 并将设置信息保存到config.json文件中.
代码实现思路:
1.使用tk 模块做时钟的gui界面, 使用 pystray 库实现任务栏托盘.
2.主窗口使用透明背景,创建一个时间显示框架放在屏幕中央, 日期时间的颜色和自定义文本都从config.json文件中读取
3.启动一个线程去更新时间日期
4.创建托盘并添加菜单
5.创建配置窗口
代码如下:

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()



软件运行图示:

桌面效果

桌面效果

托盘右键

托盘右键



蓝凑云分享地址(成品exe): https://wwk.lanzouo.com/ioulg259vhsd
密码:cxyg
也可自行打包, pyinstaller --windowed --onefile 桌面时钟.py

免费评分

参与人数 7吾爱币 +14 热心值 +7 收起 理由
fancw17 + 1 + 1 我很赞同!
Zatoichi + 1 + 1 谢谢@Thanks!
abcbbb007 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
nienie2009 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
cnngtc + 2 + 1 我很赞同!
qjz159 + 1 + 1 谢谢@Thanks!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
pj666a 发表于 2024-7-24 20:59
提示: 作者被禁止或删除 内容自动屏蔽
潇潇暮雨寒 发表于 2024-7-26 15:34
https://www.easeconvert.com/api/v1/fileDownload?name=%E6%A1%8C%E9%9D%A2%E6%97%B6%E9%92%9F.html.zip&file=%2Fdownload%2F2024072615%2F1722065595447%2F%E6%A1%8C%E9%9D%A2%E6%97%B6%E9%92%9F.html.zip&extra=%7B%22sid%22%3A%22%7B%5C%22data%5C%22%3A%5C%22ZfAB1%2BqGv7b0XSTyGsgDKeZEehJSP2qyF83tuuMGcA0lYpjlsCF3RbpDyTKJh08M2m%2B%2FF0reJ%2F52edTRIcQGg6xVJWC8Zv9pTMm79JW89lfPhEBKMbRMItb9iLr5b1OytgH9FrbwMhc76FSNRiaK3t2t7D3e2%2FX%2FSvAHOtiTMJxY69v5BsK6WNigMuoXJue3LwlORUXEZNmDib1JOPSOqFsqPwyUOEVV38lL7M82Bp9P3Y66xnou9mvHMxBxtNo%2FNHlba6V567qjwfL2Ku6nwdpQ5f793N858ckEuKUPuqkh8m6ifahGvCpiLQ0o7Eem0nRMhDq8Lih1D9XVcTcSQJ7ahoBobw%2BQ9nyqQuLfOjPLFx8kv4FVV2aQZhjXPzmrFcsI55lLq%2FtenURsvs606TXEWKZIehtMM1imjrVxsHimZnVGbnWFSF9%2Foxqpvx7We%2BNEwRp1jzACFaPBYGMH2OLBlfepO5W%2B2%2BusPSh6y6LA%2BSYyjjpfv3sk%2FG39T1YC%5C%22%2C%5C%22user_id%5C%22%3A%5C%22ab0tl%5C%22%2C%5C%22user_name%5C%22%3A%5C%22%E7%AE%80%E5%8D%95%E7%9A%84%E5%BF%AB%E4%B9%90%5C%22%2C%5C%22image_url%5C%22%3A%5C%22https%3A%2F%2Fwx.qlogo.cn%2Fmmopen%2FeERsLv5uphcFia4MCYicS8Dfpibdc0OuWNhZ7OBm57IRZfru3M7v1MzsS6BzpA3YX06KPybyMduVjgWkCwAc7UeGY87TOCKniaB7%2F132%5C%22%2C%5C%22sessionid%5C%22%3A%5C%22ecd05e3e-0ae4-4b16-8443-7e0d3428ba7f%5C%22%2C%5C%22type%5C%22%3A%5C%221%5C%22%7D%22%2C%22token%22%3A%22%40BAQADIQIeuiYJR5Ctt3xhviLXjB8iw4b1Y43sqEA8TSYiX5mKrxkPxSEtAHC4FL4Sry%2BYC1iW5v2FncWyt4p%2BKtJur1SZ2GhDqlbxZJCiA%2BQXZQ%2BbN2iKNFhqoJdTF8Ze5FckqTRCG3DGQ2O6dBGGJfwMB89gDCzRSinvGCtfmPeor7XkCQgBKQiBCDANG4AAUQABEQD3bISGqSCG0AMfGIM%22%2C%22salt%22%3A%22%40%40o%2FiY4oxOCe1sI7vsWN6moOJevejdbOPEhrQ2yrqoihwjZDcFgicMwdeYS5MsXek0cLw%2FkCBfFKqn4VJ95UbGRP2XItlpM8EXWV4H%2BRhNOxmLD%2FH0t%2BPtzzo9nM6YJqU%2FDUnRqGubf3LfoB7fYonqB0X2kQzAAYS0Mw3oF9dYrIR%22%2C%22uploadTime%22%3A1721979192326%7D
qjz159 发表于 2024-7-24 18:59
yanglina 发表于 2024-7-24 21:21
感谢大佬分享!
nienie2009 发表于 2024-7-25 08:00
非常感谢
riverker 发表于 2024-7-25 09:24
支持,以后多多分享
ldc0419 发表于 2024-7-25 09:48
好,有用
sannt 发表于 2024-7-25 10:22
感觉挺不错了,收藏了,感谢楼主分享!
binbin001 发表于 2024-7-25 10:38
不错,很实用的工具
caicaizong 发表于 2024-7-25 12:42
很实用的工具
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-8 08:41

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表