我做了一个输入小部件,它获得输入的数字,当他们点击确定(这成为函数重复(n))的秒数),我想每n秒调用一次该函数。点击OK后,我想要一个按钮在屏幕上显示,以防止该功能重复发生。我怎样才能做到这一点?在一定的时间后通话功能,并在按钮上按下按钮,tkinter
这是我有:
def ok_func():
global stop
stop = 1
timer_pop.destroy()
seconds = seconds_entry.get()
seconds = int(seconds)
stop_timer.grid()
while stop == 1:
stop_timer.config(highlightbackground=mycolor)
background()
time.sleep(seconds)
这是我对停止计时器按钮:
def stopTimer():
global stop
stop = 0
stop_timer.grid_forget()
感谢 编辑:
global counter
counter = 0
def ok_func():
global stop_timer
print('function: "ok" is running now.')
global counter
counter += 1
def stopTimer():
global recur
stop_timer.destroy()
timer_pop.after_cancel(recur)
try:
if counter == 1:
global time
time = int(seconds_entry.get())
timer_pop.destroy()
stop_timer = Button(app, text="Stop Timer", command=stopTimer)
stop_timer.grid(row=0, column=6, padx=10)
#stop_timer.config(highlightbackground=ok_func)
global recur
print ('function will start again in', time, 'seconds')
recur = app.after(1000*time, background)
else:
print ('function will start again in', time, 'seconds')
recur = app.after(1000*time, background)
#stop_timer.config(highlightbackground=mycolor)
except ValueError:
counter = 0
print("thats not a number")
我试着你说了什么,但仍然没有工作。颜色只改变一次,然后停止。此外,我想停止按钮来更改背景背景,但它不起作用。谢谢你的帮助。
不使用'time.sleep' ...你会杀了你的应用程序。使用'tkinter.Widget.after'。见例如http://stackoverflow.com/a/12043756/748858和http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method – mgilson 2014-10-01 23:50:23