代码:
import tkinter as tk from time import time print("hello world") class TimeCounter: def __init__(self): root = tk.Tk() root.title("计时器") root.geometry('800x600') self.display = tk.Label(root, text="00:00", , font=('微软雅黑', 50), fg="red") self.display.pack() self.button_start = tk.Button(root, text='start', command=self.start) self.button_start.pack() self.Varmin = tk.StringVar() self.entrymin = tk.Entry(root, textvariable = self.Varmin) self.entrymin.pack() self.Varsec = tk.StringVar() self.entrysec = tk.Entry(root, textvariable = self.Varsec) self.entrysec.pack() self.paused = True root.mainloop() # 进入消息循环 def start(self): if self.paused: self.oldtime = time() self.paused = False self.run_timer() def gettime(self): try: min = self.entrymin.get() min = int(min) except: min = 0 try: sec = self.entrysec.get() sec = int(sec) except: sec = 0 self.minsec = 60*min+sec return self.minsec def run_timer(self): self.minsec = self.gettime() self.deltas = time() - self.oldtime # 正向计时 self.deltas1 = self.minsec - self.deltas print(self.deltas) print(self.deltas1) if self.deltas1>0: deltasstr = '{:.0f}:{:.3f}'.format(*divmod(self.deltas1,60)) #用 * 拆分这个元组 else: deltasstr = '00:00' print(deltasstr) self.display.config(text = deltasstr) #更新 text self.display.after(1,self.run_timer) # 间隔1毫秒再次执行run_timer函数,after循环定时器 TimeCounter()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理