此代码是模拟智能烤面包机的程序的开始。我正在为我的倒数计时器做一个取消按钮。计时器工作正常,但按下取消按钮时会显示消息“计时器已完成”,但计时器仍在继续。有没有办法可以做到这一点?
from tkinter import *
import time
class Window(Frame): #creating window
def __init__(self, master = None): #defining master window
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("GUI")
self.pack(fill=BOTH, expand=1) #allowing size of window to be changed during running of program
#using a slider instead of a dial as no dial in tkinter
timerinputDial = Scale(self, from_=0, to=6, tickinterval=1, showvalue=0)
timerinputDial.place(x=200, y=20)
#Timer
def count_down():
for t in range(((timerinputDial.get())*60), -1, -1): #starts with the value of the input dial
# format as 2 digit integers, fills with zero to the left
# divmod() gives minutes, seconds
sf = "{:02d}:{:02d}".format(*divmod(t, 60))
#print(sf)
time_str.set(sf)
root.update()
# delay one second
time.sleep(1)
if t == 0:
time_str.set(timer_done) #telling user timer is finished
timerinputDial.set(0) #setting dial back to 0
return
def cancel():
time_str.set(timer_done) #telling user timer is finished
timerinputDial.set(0) #setting dial back to 0
return
time_str = StringVar()
timer_done = "Timer done"
# creating the time display label, giving it a large font
# labelling auto-adjusts to the font
label_font = ('helvetica', 40)
Label(root, textvariable=time_str, font=label_font, bg='white',
fg='blue', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
# creating start and stop buttons
# pack() positions the buttons below the label
startButton = Button(root, text='Start', command=count_down).pack()
# stop simply exits root window
cancelButton = Button(root, text='Cancel', command=cancel).pack()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
我刚才做了一个简单的启动/停止计时器。它只是测试一个实例变量来启动和停止。请注意,您应该使用after()
而不是time.sleep()
来避免循环停止更新其他所有内容。
from tkinter import *
class TimerTest():
def __init__(self, root):
self.root=root
self.is_running=False
self.count=IntVar()
self.max_seconds=60 ## quit after this amount of time
Label(root, textvariable=self.count,
font=('DejaVuSansMono', 12, "bold"),
bg="lightyellow").grid(row=1, column=0,
columnspan=2, sticky="ew")
Button(root, text="Start", fg="blue", width=15,
command=self.startit).grid(row=10,
column=0, sticky="nsew")
Button(root, text="Stop", fg="red", width=15,
command=self.stopit).grid(row=10,
column=1, sticky="nsew")
Button(self.root, text="Quit", bg="orange",
command=self.root.quit).grid(row=11,
column=0, columnspan=2, sticky="nsew")
def startit(self):
if not self.is_running: ## avoid 2 button pushes
self.is_running=True
self.increment_counter()
def increment_counter(self):
if self.is_running: ## stopit not called
c=self.count.get() +1
self.count.set(c)
if c < self.max_seconds:
self.root.after(1000, self.increment_counter) ## every second
else:
self.is_running=False
Label(root, text="Time Is Up",
font=('DejaVuSansMono', 14, "bold"),
bg="red").grid(row=5, column=0,
columnspan=2, sticky="ew")
def stopit(self):
self.is_running = False
root = Tk()
TT=TimerTest(root)
root.mainloop()