import tkinter as ttk
import random, time
def question1(*args):
waitmsg= "You must wait for " + str(random.randrange(20,240,5)) + " minutes"
ttk.Label(mainframe, text=(waitmsg), width=30).grid(column=1, row=3)
countdown()
# go do a bunch of stuff
def question2(*args):
waitmsg= "WAIT " + str(root.wait_time) + " minutes"
ttk.Label(mainframe, text=(waitmsg), width=30).grid(column=1, row=3)
countdown()
def countdown(*args):
# intended to display the remaining time as it counts down
# the print works right away (no dealy!)
print (root.wait_time)
# the Label waits for a full 5 seconds then displays a 0 wait time!
waitmsg= "WAIT count " + str(root.wait_time) + " minutes"
ttk.Label(mainframe, text=(waitmsg), width=30).grid(column=1, row=3)
if root.wait_time >= 1 :
root.wait_time -= 1
root.after(1000, countdown())
else:
root.wait_time=5
return
root = ttk.Tk()
root.title("Choose question!")
root.wait_time = 0
mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
if root.wait_time == 0:
## root.wait_time= random.randrange(20,240,5)
root.wait_time=5
ttk.Button(mainframe, text="question2?", command=question2).grid(column=4, row=1)
ttk.Button(mainframe, text="question1?", command=question1).grid(column=1, row=1)
root.bind('<Return>', question2)
root.mainloop()
答案 0 :(得分:1)
主要问题是您将countdown()的结果用作语句root.after(1000,countdown())中的回调函数。您应该只传递函数名称:root.after(1000,countdown)。 – acw1668
这似乎可以解决问题。谢谢!
看来还有很多关于tkinter和stackoverflow的知识:-)