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()

推荐阅读