python 控件显示时间_设置tkinter标签以显示倒计时时间
我正在嘗試做一個(gè)pomodoro計(jì)時(shí)器,它將顯示不同的倒計(jì)時(shí)點(diǎn)擊三個(gè)按鈕選項(xiàng)之一。在
這里的問題是,每次我在之前單擊一個(gè)按鈕之后單擊一個(gè)按鈕,標(biāo)簽都會(huì)為顯示哪個(gè)計(jì)時(shí)器而煩惱。它試圖同時(shí)顯示兩個(gè)定時(shí)器倒計(jì)時(shí)。在
當(dāng)我點(diǎn)擊另一個(gè)按鈕時(shí),我需要標(biāo)簽停止顯示第一個(gè)按鈕的定時(shí)器倒計(jì)時(shí)。我的代碼是: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()
總結(jié)
以上是生活随笔為你收集整理的python 控件显示时间_设置tkinter标签以显示倒计时时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python construct 字符串
- 下一篇: wp转shp_【收藏】空间数据格式转换方