Source file
01 import tkinter 02 import time 03 04 star=time.time() 05 06 def gettime(): 07 elap=time.time()-star# 获取时间差 08 hours=int(elap/3600) 09 minutes = int((elap-hours*3600)/60) 10 seconds = int(elap-minutes*60.0-hours*3600) 11 var.set('%02d:%02d:%02d' %(hours, minutes, seconds)) 12 root.after(1000, gettime)# 每隔1s调用函数自身获取时间 13 14 root = tkinter.Tk() 15 root.title('考试计时器') 16 root.geometry('1000x400') 17 var=tkinter.StringVar() 18 tkinter.Label(root,text='考试计时',font='black 50 bold').pack(side='top'); 19 lb=tkinter.Label(root, textvariable=var, fg='red', font=("微软雅黑", 150))# 设置字体大小颜色 20 lb.pack(side='top') 21 #Quit按钮退出;Run按钮打印计算结果 22 tkinter.Button(root, text='退出', command=root.quit).pack(side='left') 23 tkinter.Button(root, text='运行', command=gettime).pack(side='right') 24 root.mainloop()