python 3.x - Tkinter Alarm Clock



我正在尝试用tkinter建立一个倒计时计时器。我想将条目中的值传递给倒计时(count)函数。下面是我的尝试:

def countdown(count): 
    label['text'] = count
    if count > 0:
        top.after(1000, countdown,count-1)
top = tkinter.Tk()
top.geometry("700x100")
hoursT=tkinter.Label(top, text="Hours:")
hoursE=tkinter.Entry(top)
minuteT=tkinter.Label(top, text="Minutes:")
minuteE=tkinter.Entry(top)
secondT=tkinter.Label(top, text="Seconds:")
secondE=tkinter.Entry(top)
hoursT.grid(row=1,column=1)
hoursE.grid(row=1,column=2)
minuteT.grid(row=1,column=3)
minuteE.grid(row=1,column=4)
secondT.grid(row=1,column=5)
secondE.grid(row=1,column=6)
label = tkinter.Label(top)
label.grid(row=3)
t=(int(hoursE.get())*360+int(minuteT.get())*60+int(secondE.get())
button=tkinter.Button(top,text="Start Timer",command=lambda        count=t:countdown(count))
button.grid(row=2)

但是,我得到这个错误:

Traceback (most recent call last):
File "C:Userscharley.ACER-PCAppDataLocalProgramsPythonPython35-    32tkinterTutorial.py", line 30, in <module>
t=(int(hoursE.get())*360+int(minuteT.get())*60+int(secondE.get()))
ValueError: invalid literal for int() with base 10: ''

如何运行这段代码:

t=(int(hoursE.get())*360+int(minuteT.get())*60+int(secondE.get())

仅当条目被整数填充时?

谢谢:)

一种解决方案是首先将按钮放在那里,然后在运行时使用事件侦听器更改命令:

button=tkinter.Button(top,text="Start Timer",command=lambda:None)
button.grid(row=2)
def updateButton():
    hour,min,sec=hoursE.get(),minuteT.get(),secondE.get()
    if hour.isdigit() and min.isdigit() and sec.isdigit():
        time=int(hour)*360+int(min)*60+int(sec)
        button.configure(command=lambda count=time:countdown(count))
for widget in (hoursE,minuteT,secondE):
    widget.bind("<FocusOut>", updateButton)

最新更新