簡體   English   中英

如何在 tkinter 倒數計時器上實現啟動、停止和重置功能?

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

此代碼根據選擇的按鈕運行 25 分鍾、5 分鍾或 10 分鍾倒數計時器。 如何使正在運行的計時器暫停、恢復和重置? 我需要幫助為這三個功能編寫回調。 它們將鏈接到窗口頂部的三個按鈕。 這是我的代碼如下:

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()

您的問題對於單擊三個按鈕中的每一個時您想要發生的確切情況仍然有些模糊,所以我不得不猜測。 為了支持執行這些操作的能力,需要添加更多實例屬性來跟蹤應用程序的狀態——即self._pausedself._starttime 可以根據需要在各種方法中檢查和更新這些。

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