【发布时间】:2014-10-01 23:45:40
【问题描述】:
我制作了一个条目小部件,当他们单击确定时它会获取输入的数字(这将成为函数 recur(n) 的秒数),我想每隔 n 秒调用一次函数。单击“确定”后,我希望在屏幕上显示一个按钮,该按钮将停止该功能的重复。我该怎么做?
这就是我所拥有的:
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。参见例如stackoverflow.com/a/12043756/748858 和 effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method
标签: python function python-2.7 tkinter