设置tkinter标签以显示倒计时时间

2024-06-08 22:53:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一个pomodoro计时器,它将显示不同的倒计时点击三个按钮选项之一。在

这里的问题是,每次我在之前单击一个按钮之后单击一个按钮,标签都会为显示哪个计时器而烦恼。它试图同时显示两个定时器倒计时。在

当我点击另一个按钮时,我需要标签停止显示第一个按钮的定时器倒计时。我的代码是:

from tkinter import *

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

    def createWidgets(self):

        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):
        countdown(1500)

    def shortBreak(self):
        countdown(300)

    def longBreak(self):
        countdown(600)





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



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

Tags: textselfapplicationdefbuttonrootleft按钮
1条回答
网友
1楼 · 发布于 2024-06-08 22:53:51

这里有一个可能的解决方案。这个函数将countdown函数移到Application类中,并使其自由运行。然后我添加了一个class属性来跟踪当前剩余时间,并将其递减为0。这确实有一个缺点,即可能需要几乎一秒钟的时间来显示新的计数。在

from tkinter import *
from threading import Event

class Application(Frame):
    def __init__(self,master):
        super(Application,self).__init__(master)
        self.pack()
        self.createWidgets()
        self._count = 0
        self.countdown()

    def createWidgets(self):

        self.labelvariable = StringVar()

        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):
        self._count = 1500

    def shortBreak(self):
        self._count = 300

    def longBreak(self):
        self._count = 600

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



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

这是另一个基于Curly Joe的comment的选项。它使用after_cancel来取消下一个报警,从而停止当前正在运行的倒计时。它的好处是新的倒计时立即开始。在

^{pr2}$

相关问题 更多 >