網(wǎng)站首頁 編程語言 正文
本文實(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
相關(guān)推薦
- 2022-04-28 C++中的友元函數(shù)與友元類詳情_C 語言
- 2022-05-20 Tomcat下載安裝配置詳細(xì)過程
- 2023-06-17 Queue隊(duì)列中join()與task_done()的關(guān)系及說明_python
- 2022-08-20 在python中讀取和寫入CSV文件詳情_python
- 2023-03-28 Python代碼庫之Tuple如何append添加元素問題_python
- 2022-11-06 react中關(guān)于Context/Provider/Consumer傳參的使用_React
- 2022-07-07 Python實(shí)現(xiàn)8個概率分布公式的方法詳解_python
- 2022-06-06 解決:Access denied for user ‘root‘@‘localhost‘ (usin
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支