脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python编写一个GUI倒计时器

python编写一个GUI倒计时器

2022-08-10 09:11苦逼工科男 Python

这篇文章主要为大家详细介绍了python编写一个GUI倒计时器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现GUI倒计时器的具体代码,供大家参考,具体内容如下

python编写一个GUI倒计时器

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

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

延伸 · 阅读

精彩推荐