2016-06-25 4 views
0

Ich versuche einen Countdown-Timer mit Tkinter zu bauen. Ich möchte die Werte aus den Einträgen auf die Countdown-Funktion (countdown) übertragen. Hier ist, was ich versuche:Tkinter Wecker

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) 

Allerdings bekomme ich diesen Fehler:

Traceback (most recent call last): 
File "C:\Users\charley.ACER-PC\AppData\Local\Programs\Python\Python35- 32\tkinterTutorial.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: '' 

Wie kann ich diesen Code ausführen:

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

nur dann, wenn die Einträge mit Zahlen gefüllt?

Thanks :)

Antwort

0

Eine Lösung ist die Taste dort an erster Stelle und change the command zur Laufzeit mit event listeners haben:

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)