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()
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Python GUI 圖形用戶界面
- 基於Python制作一個多進制轉換工具
- Python GUI編程詳解
- python tkinter庫的Text記錄點擊路經和刪除記錄詳情
- python3.7.2 tkinter entry框限定輸入數字的操作