无法在Python和Tkinter中停止计时器程序

时间:2017-11-14 14:31:00

标签: python tkinter

我在python中有一个带有Tkinter GUI的计时器程序。我能够运行计时器用户输入,但是我无法从tkinter按钮暂停或停止计时器。我尝试创建一个变量,当按下停止按钮以在我的if语句中启动time.sleep()时更改该变量,但没有运气。有什么想法吗?

from tkinter import Tk, Label, StringVar
from tkinter.ttk import Button, Entry

class Timer:
    def __init__(self, master):
        self.seconds = 0
        self.f = False
        self.time = StringVar()
        self.time_label = Label(master, relief='flat', font=("Cambria", 20),
                            textvariable=self.time)

        self.hr = StringVar()
        self.hr_label = Label(master, text='Hours:').grid(row=1, column=1, padx=5, pady=1)
        self.entry_hr = Entry(master, textvariable=self.hr, width=4)
        self.entry_hr.grid(row=1, column=2)

        self.mins = StringVar()
        self.min_label = Label(master, text='Minutes:').grid(row=2, column=1, padx=5, pady=1)
        self.entry_min = Entry(master, textvariable=self.mins, width=4)
        self.entry_min.grid(row=2, column=2)

        self.secs = StringVar()
        self.secs_label = Label(master, text='Seconds:').grid(row=3, column=1, padx=5, pady=1)
        self.entry_sec = Entry(master, textvariable=self.secs, width=4)
        self.entry_sec.grid(row=3, column=2)

        self.time.set('00:00:00')
        self.start_button = Button(master, text='Start')
        self.start_button.bind('<Button-1>', self.start_countdown)
        self.stop_button = Button(master, text='Stop')

        self.time_label.grid(row=0, columnspan=4, padx=30, pady=10)
        self.start_button.grid(row=4, columnspan=4, pady=(10,20))
        self.stop_button.grid(row=5, columnspan=4, pady=(10,20))

    def set_time(self, hours, minutes, seconds):
        self.seconds = hours * 3600 + minutes * 60 + seconds
        self.time.set(self.format_time(self.seconds))

    def start_countdown(self, event):
        f = False
        h = self.entry_hr.get()
        m = self.entry_min.get()
        s = self.entry_sec.get()
        h,m,s = map(lambda x: int(x) if x else 0, (h,m,s))
        self.set_time(h,m,s)
        self.countdown()

    def stop(self):
        global f
        f= True

    def countdown(self):
        if self.seconds <= 0:
        return
        else: 
            self.end_label = Label( text ="Time's up")
        if self.f == True:
            time.sleep()
        self.time.set(self.format_time(self.seconds))
        self.seconds -= 1
        self.time_label.after(1000, self.countdown)

    def format_time(self, seconds):
        h = seconds // 3600
        m = (seconds - h*3600) // 60
        s = seconds - h*3600 - m*60
        return '{:0>2}:{:0>2}:{:0>2}'.format(h,m,s)

if __name__ == '__main__':
    root = Tk()
    timer = Timer(root)
    root.mainloop()

1 个答案:

答案 0 :(得分:2)

首先,您的Button小部件self.stop_button没有与之关联的命令或绑定,因此它实际上并没有做任何事情。

其次,即使它确实调用了函数,您还要更改全局变量f的值,但在if语句中,您引用了局部变量{{1 (self.f)。

第三,你没有正确地调用Timer.f,它需要一个以秒为单位的值来休眠。

第四,time.sleep()只会延迟函数在你指定的时间段内运行,并最终导致计时器再次开始滴答。

相反,您应该执行以下操作(将time.sleep()窗口小部件与函数Button相关联之后):

self.stop()

这意味着每次我们循环执行该函数时,我们要做的第一件事就是检查def stop(self): self.f = True def countdown(self): if self.f == False: print(self.f) if self.seconds <= 0: return else: self.end_label = Label( text ="Time's up") self.time.set(self.format_time(self.seconds)) self.seconds -= 1 self.time_label.after(1000, self.countdown) 是否为self.f,如果它不是,那么计时器就会停在那里。< / p>