【Python】tkinter 构建桌面时钟(Desktop Time Clock)【支持秒显示】

Python 构建Desktop Time Clock【支持秒显示】

起因

由于部分电脑只能显示时间的时分,不支持显示秒,利用Pythontkinter库做了一个桌面时钟,该时钟能够获取系统实时时钟,并精确到秒显示

附注:本程序每1s才会更新一次时间变量,所占用系统资源极少,可长时间运行

设置自启动

  1. 创建 DesktopTimeClock.bat 文件,文件里写入如下内容:
    1.1 假设你的文件存放在C:\\Desktop\\DesktopTimeClock.py
    1.2 以下文件路劲和文件名需要根据个人实际情况进行替换
cd C:\\Desktop
python DesktopTimeClock.py
  1. 运行DesktopTimeClock.bat: 双击运行 DesktopTimeClock.bat 文件,桌面时钟将会出现在您的屏幕上。

  2. 创建快捷方式:右键点击 DesktopTimeClock.bat 文件,选择“创建快捷方式”。

  3. 添加到启动文件夹: 将快捷方式移动到启动文件夹(C:\Users<用户名>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup)中。

  4. 完成: 现在,每次启动电脑时,桌面时钟都会自动显示。

  5. 关闭程序: 点击桌面时钟窗口,并按住Esc1s即可关闭程序。

代码实现

  • 时钟的位置和字体大小可根据个人需要自行调节
from tkinter import *
import time, datetime
from time import gmtime, strftime

root = Tk()

# Window Attributes
root.overrideredirect(1)  # 隐藏窗口边框
root.wm_attributes("-transparentcolor", "gray99")  # 设置透明背景色

running = True  # 运行状态

# 关闭窗口函数
def close(event):
    global running
    running = False

root.bind('<Escape>', close)  # 绑定Esc键关闭窗口

screen_width = root.winfo_screenwidth()  # 获取屏幕宽度
screen_height = root.winfo_screenheight()  # 获取屏幕高度

timeframe = Frame(root, width=screen_width, height=screen_height, bg="gray99")  # 创建主框架
timeframe.grid(row=0,column=0)

tkintertime = StringVar()  # 创建时间变量
timelabel = Label(timeframe, textvariable=tkintertime, fg="white", bg="gray99", font=("NovaMono", 40))  # 创建时间标签
# timelabel.place(y=screen_height/2 - 30, x=screen_width/2, anchor="center")  # 设置时间标签位置(屏幕中心)
timelabel.place(y=screen_height/4 - 30, x=screen_width-screen_width/4, anchor="center")  # 设置时间标签位置(右上角)

tkinterdate = StringVar()  # 创建日期变量
datelabel = Label(timeframe, textvariable=tkinterdate, fg="white", bg="gray99", font=("Bahnschrift", 15))  # 创建日期标签
# datelabel.place(y=screen_height/2 + 30, x=screen_width/2, anchor="center")  # 设置日期标签位置(屏幕中心)
datelabel.place(y=screen_height/4 + 30, x=screen_width-screen_width/4, anchor="center")  # 设置日期标签位置(右上角)

while running:
    tkintertime.set(value=strftime("%H:%M:%S"))  # 更新时间
    tkinterdate.set(value=strftime("%A, %e %B"))  # 更新日期
    root.update_idletasks()  # 更新窗口
    root.update()  # 更新窗口
    time.sleep(1)  # 延迟1秒

相关推荐

  1. 构建一个动态时钟

    2024-07-12 15:48:03       49 阅读
  2. git 查看最新commit提交时间(具体到时分

    2024-07-12 15:48:03       66 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 15:48:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 15:48:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 15:48:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 15:48:03       69 阅读

热门阅读

  1. 使用GeographicLib在C++中进行地理坐标转换

    2024-07-12 15:48:03       22 阅读
  2. 使用Gunicorn提高Web应用的多核并发处理能力

    2024-07-12 15:48:03       25 阅读
  3. Vue使用socket实现实时通信

    2024-07-12 15:48:03       26 阅读
  4. golang使用migrate迁移pg数据库表报错处理

    2024-07-12 15:48:03       23 阅读
  5. C#,开发过程中技术点GPT问答记录

    2024-07-12 15:48:03       20 阅读
  6. 学生管理系统(残缺版)

    2024-07-12 15:48:03       22 阅读
  7. IPython多核并行编程指南:并发任务处理

    2024-07-12 15:48:03       23 阅读
  8. 【LeetCode】快乐数

    2024-07-12 15:48:03       22 阅读
  9. 数据分析如何正确地学习与使用

    2024-07-12 15:48:03       22 阅读