简体   繁体   English

如何在 tkinter 倒数计时器上实现启动、停止和重置功能?

[英]How do I implement start, stop and reset features on a tkinter countdown timer?

This code runs 25 minute, 5 minute, or 10 minute countdown timer based on which button is chosen.此代码根据选择的按钮运行 25 分钟、5 分钟或 10 分钟倒数计时器。 How can I make a running timer pause,resume, and reset?如何使正在运行的计时器暂停、恢复和重置? I need help writing callbacks for these three functionalities.我需要帮助为这三个功能编写回调。 They are to be linked to the three buttons up at the top of the window.它们将链接到窗口顶部的三个按钮。 Here's my code below:这是我的代码如下:

from tkinter import *

class Application(Frame):
    def __init__(self,master):
        super(Application,self).__init__(master)
        self.pack()
        self.createWidgets()
        self._alarm_id = None

    def createWidgets(self):
        self.someFrame = Frame(self)
        self.startButton = Button(self.someFrame, text="Start",command=self.startTime)
        self.startButton.pack(side=LEFT)

        self.stopButton = Button(self.someFrame, text="Stop", command=self.stopTime)
        self.stopButton.pack(side=LEFT)

        self.resetButton = Button(self.someFrame, text="Reset", command=self.resetTime)
        self.resetButton.pack(side=LEFT)
        self.someFrame.pack(side=TOP)

        self.labelvariable = StringVar()
        self.labelvariable.set("25:00")

        self.thelabel = Label(self,textvariable = self.labelvariable,font=('Helvetica',50))
        self.thelabel.pack(side=TOP)

        self.firstButton = Button(self,text="pomodoro",command=self.pomodoro)
        self.firstButton.pack(side=LEFT)

        self.secondButton = Button(self,text="short break",command=self.shortBreak)
        self.secondButton.pack(side=LEFT)

        self.thirdButton = Button(self,text="long break",command=self.longBreak)
        self.thirdButton.pack(side=LEFT)

    def pomodoro(self):
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
        self.countdown(1500)

    def shortBreak(self):
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
        self.countdown(300)

    def longBreak(self):
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
        self.countdown(600)


    def startTime(self):
        pass

    def stopTime(self):
        pass

    def resetTime(self):
        pass

    def countdown(self, timeInSeconds):
        mins,secs = divmod(timeInSeconds, 60)
        timeformat = "{0:02d}:{1:02d}".format(mins, secs)
        app.labelvariable.set(timeformat)
        self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds-1)


if __name__ == '__main__':
    root = Tk()
    root.title("Timer")
    app = Application(root)
    root.mainloop()

Your question is still a little vague about precisely what you want to happen when each of the three buttons is clicked, so I had to guess.您的问题对于单击三个按钮中的每一个时您想要发生的确切情况仍然有些模糊,所以我不得不猜测。 To support the ability to do these things required adding a couple more instance attributes to keep track of the state of the application—namely self._paused and self._starttime .为了支持执行这些操作的能力,需要添加更多实例属性来跟踪应用程序的状态——即self._pausedself._starttime These can be checked and updated in the various methods as necessary.可以根据需要在各种方法中检查和更新这些。

from tkinter import *

class Application(Frame):
    def __init__(self,master):
        super(Application,self).__init__(master)
        self.pack()
        self.createWidgets()
        self._alarm_id = None
        self._paused = False
        self._starttime = 25 * 60

    def createWidgets(self):
        self.someFrame = Frame(self)
        self.startButton = Button(self.someFrame, text="Start",command=self.startTime)
        self.startButton.pack(side=LEFT)

        self.stopButton = Button(self.someFrame, text="Stop", command=self.stopTime)
        self.stopButton.pack(side=LEFT)

        self.resetButton = Button(self.someFrame, text="Reset", command=self.resetTime)
        self.resetButton.pack(side=LEFT)
        self.someFrame.pack(side=TOP)

        self.labelvariable = StringVar()
        self.labelvariable.set("25:00")

        self.thelabel = Label(self,textvariable = self.labelvariable,font=('Helvetica',50))
        self.thelabel.pack(side=TOP)

        self.firstButton = Button(self,text="pomodoro",command=self.pomodoro)
        self.firstButton.pack(side=LEFT)

        self.secondButton = Button(self,text="short break",command=self.shortBreak)
        self.secondButton.pack(side=LEFT)

        self.thirdButton = Button(self,text="long break",command=self.longBreak)
        self.thirdButton.pack(side=LEFT)

    def pomodoro(self):
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
        self.countdown(1500)

    def shortBreak(self):
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
        self._paused = False
        self.countdown(300)

    def longBreak(self):
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
        self._paused = False
        self.countdown(600)

    def startTime(self):
        """ Resume """
        self._paused = False
        if self._alarm_id is None:
            self.countdown(self._starttime)

    def stopTime(self):
        """ Pause """
        if self._alarm_id is not None:
            self._paused = True

    def resetTime(self):
        """ Restore to last countdown value. """
        if self._alarm_id is not None:
            self.master.after_cancel(self._alarm_id)
            self._alarm_id = None
            self._paused = False
            self.countdown(self._starttime)
            self._paused = True

    def countdown(self, timeInSeconds, start=True):
        if start:
            self._starttime = timeInSeconds
        if self._paused:
            self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds, False)
        else:
            mins, secs = divmod(timeInSeconds, 60)
            timeformat = "{0:02d}:{1:02d}".format(mins, secs)
            app.labelvariable.set(timeformat)
            self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds-1, False)


if __name__ == '__main__':
    root = Tk()
    root.title("Timer")
    app = Application(root)
    root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM