前言

这里是2025年的天影大侠,这东西是网课的时候做的,把钉钉放着,自己却在翻python文档,好神奇。

正文

分享用Python做的一个桌面时钟
桌面时钟
代码如下,有注释,一目了然:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#引入一些东西
import tkinter as tk
from tkinter import messagebox
import time
#设置主窗口
root = tk.Tk()
#设置窗口标题
root.title('桌面时钟')
#设置窗口透明度
root.attributes("-alpha",0.95)
#设置窗口置顶
root.attributes('-topmost', True)
#设置一些有用的变量
#size,color,font可自行修改
size = 40
width = size/4*25
height = size/4*8
color = "black"
font = "微软雅黑"
#将窗口显示到屏幕正中央并设置窗口大小
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size_geo = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
root.geometry(size_geo)
root.resizable(0,0)
# 获取时间的函数
def gettime():
# 获取当前时间
dstr.set(time.strftime("%H:%M:%S"))
# 每隔 1s 调用一次 gettime()函数来获取时间
root.after(1000, gettime)
# 生成动态字符串
dstr = tk.StringVar()
# 利用 textvariable 来实现文本变化
lb = tk.Label(root,textvariable=dstr,fg=color,font=(font,size))
#把时钟放上窗口
lb.pack()
# 调用生成时间的函数
gettime()
# 显示窗口
root.mainloop()

拿着用用没问题