文章目录

原本这次实践是想看看翻译的,但是发现一众插件好像都有点问题,不知道是不是谷歌翻译的接口有变化了。

那这次就干脆简单实现个带皮肤的电子钟吧。

先确保依赖模块装了,这次是需要 tkinter

1
pip install tkinter

首先引入模块 tkintertime

1
2
import tkinter as tk
from time import strftime

然后定义好界面样式,包括主题。

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
def light_theme():
frame = tk.Frame(root, bg="white")
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)
lbl_1 = tk.Label(frame, font=('calibri', 40, 'bold'), background='White', foreground='black')
lbl_1.pack(anchor="s")

def time():
string = strftime('%I:%M:%S %p')
lbl_1.config(text=string)
lbl_1.after(1000, time)
time()

def dark_theme():
frame = tk.Frame(root, bg="#22478a")
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)
lbl_2 = tk.Label(frame, font=('calibri', 40, 'bold'), background='#22478a', foreground='white')
lbl_2.pack(anchor="s")

def time():
string = strftime('%I:%M:%S %p')
lbl_2.config(text=string)
lbl_2.after(1000, time)
time()

root = tk.Tk()
root.title("电子钟")
canvas = tk.Canvas(root, height=140, width=400)
canvas.pack()

frame = tk.Frame(root, bg='#22478a')
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)
lbl = tk.Label(frame, font=('calibri', 40, 'bold'), background='#22478a', foreground='white')
lbl.pack(anchor="s")

实现核心的显示时钟功能。

1
2
3
4
5
def time():
string = strftime('%I:%M:%S %p')
lbl.config(text=string)
lbl.after(1000, time)
time()

最后,加上切换主题的方法。

1
2
3
4
5
6
7
menubar = tk.Menu(root)
theme_menu = tk.Menu(menubar, tearoff=0)
theme_menu.add_command(label="Light", command=light_theme)
theme_menu.add_command(label="Dark", command=dark_theme)
menubar.add_cascade(label="主题", menu=theme_menu)
root.config(menu=menubar)
root.mainloop()

运行一下看看。

附上所有源码。

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
42
43
44
45
46
47
48
49
50
import tkinter as tk
from time import strftime

def light_theme():
frame = tk.Frame(root, bg="white")
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)
lbl_1 = tk.Label(frame, font=('calibri', 40, 'bold'), background='White', foreground='black')
lbl_1.pack(anchor="s")

def time():
string = strftime('%I:%M:%S %p')
lbl_1.config(text=string)
lbl_1.after(1000, time)
time()

def dark_theme():
frame = tk.Frame(root, bg="#22478a")
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)
lbl_2 = tk.Label(frame, font=('calibri', 40, 'bold'), background='#22478a', foreground='white')
lbl_2.pack(anchor="s")

def time():
string = strftime('%I:%M:%S %p')
lbl_2.config(text=string)
lbl_2.after(1000, time)
time()

root = tk.Tk()
root.title("电子钟")
canvas = tk.Canvas(root, height=140, width=400)
canvas.pack()

frame = tk.Frame(root, bg='#22478a')
frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)
lbl = tk.Label(frame, font=('calibri', 40, 'bold'), background='#22478a', foreground='white')
lbl.pack(anchor="s")

def time():
string = strftime('%I:%M:%S %p')
lbl.config(text=string)
lbl.after(1000, time)
time()

menubar = tk.Menu(root)
theme_menu = tk.Menu(menubar, tearoff=0)
theme_menu.add_command(label="Light", command=light_theme)
theme_menu.add_command(label="Dark", command=dark_theme)
menubar.add_cascade(label="主题", menu=theme_menu)
root.config(menu=menubar)
root.mainloop()

♦ 本文固定连接:https://www.gsgundam.com/archive/2022-07-15-how-to-digital-clock-python/

♦ 转载请注明:GSGundam 2022年07月15日发布于 GSGUNDAM砍柴工

♦ 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

♦ 原创不易,如果页面上有适合你的广告,不妨点击一下看看,支持作者。(广告来源:Google Adsense)

♦ 本文总阅读量