python编写一个GUI倒计时器
作者:苦逼工科男
这篇文章主要为大家详细介绍了python编写一个GUI倒计时器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了python实现GUI倒计时器的具体代码,供大家参考,具体内容如下
代码:
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", width=20, 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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。