我想用倒计时功能在Tkinter中设置一个标签。现在它所做的就是一旦达到10,就将标签设置为“10”,我真的不明白为什么。而且,即使我把定时器打印到终端上,也会显示“时间到了!”比特从不打印。
import time
import tkinter as tk
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="null")
self.label.pack()
self.countdown()
self.root.mainloop()
# Define a timer.
def countdown(self):
p = 10.00
t = time.time()
n = 0
# Loop while the number of seconds is less than the integer defined in "p"
while n - t < p:
n = time.time()
if n == t + p:
self.label.configure(text="Time's up!")
else:
self.label.configure(text=round(n - t))
app=App()
Tkinter已经有一个无限循环在运行(事件循环),并且有一种方法可以在经过一段时间后(使用
after
)调度要运行的东西。您可以利用这个优势,编写一个每秒调用一次自身的函数来更新显示。可以使用类变量来跟踪剩余时间。相关问题 更多 >
编程相关推荐