日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python編寫一個GUI倒計(jì)時器_python

作者:苦逼工科男 ? 更新時間: 2022-10-04 編程語言

本文實(shí)例為大家分享了python實(shí)現(xiàn)GUI倒計(jì)時器的具體代碼,供大家參考,具體內(nèi)容如下

代碼:

import tkinter as tk
from time import time
?
print("hello world")
?
?
class TimeCounter:
? ? def __init__(self):
? ? ? ? root = tk.Tk()
? ? ? ? root.title("計(jì)時器")
? ? ? ? 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() ? ? ?# 進(jìn)入消息循環(huán)
?
? ? 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 ? # ?正向計(jì)時
? ? ? ? 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毫秒再次執(zhí)行run_timer函數(shù),after循環(huán)定時器
?
?
TimeCounter()

原文鏈接:https://blog.csdn.net/qqyuanhao163/article/details/107678365

欄目分類
最近更新