之前发过一些tkinter有关文章,大家可以作为参考:python基于tkinter做恶搞小程序(好玩的游戏)
import tkinter as tk
import time
a = 0
t1 = time.time()
def like():
global a
a += 1
t2 = time.time()
asd['text'] = '手速:%.4f 次/秒' % (a/(t2-t1))
timelabel['text'] = '时间:%.2f 秒'% (t2-t1)
like_point['text'] = '%d 次'% a
def new():
global a
global t1
a = 0
t1 = time.time()
asd['text'] = '手速:0次/秒'
timelabel['text'] = '时间:0秒'
like_point['text'] = '0次'
# gui 图形用户界面
qwe = tk.Tk()
qwe.title('hello world')
qwe.geometry('600x600')
# 标签 label
asd = tk.Label(
qwe,
text='like',
fg='#FFFFFF',
bg='#000000',
font=(None, 40),
width=20,
height=1,
)
asd.pack()
# 时间
timelabel = tk.Label(
qwe,
text='时间:0',
fg='#FFFFFF', # 6个字符 0-255 r 255 g 255 b 255
bg='#0000FF', # 6个字符 0-255 r 0 g 0 b 255
font=('黑体',30),
width=20,
height=1,
)
timelabel.pack()
# 点赞次数
like_point = tk.Label(
qwe,
text='0次',
fg='#00AA00', # 6个字符 0-255 r 255 g 255 b 255
bg='#0000FF', # 6个字符 0-255 r 0 g 0 b 255
font=('黑体',30),
width=20,
height=1,
)
like_point.pack()
# 按钮 button
btn = tk.Button(
qwe,
text="点赞",
fg='#FFFFFF',
bg='#0000FF',
font=(None, 20),
width=10,
height=2,
command=like, # 命令
)
btn.pack()
# 重新开始
restart = tk.Button(
qwe,
text="重新开始",
fg='#FFFFFF',
bg='#0000FF',
font=(None, 20),
width=10,
height=2,
command=new, # 命令
)
restart.pack()
qwe.mainloop()
更多精彩内容