【问题标题】:call function after certain time and kill on button press, tkinter在特定时间后调用函数并在按钮按下时终止,tkinter
【发布时间】: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")

我试过你说的,但还是不行。颜色只改变一次,然后停止。另外,我希望停止计时器按钮可以用背景更改背景,但它不起作用。感谢您的帮助。

【问题讨论】:

标签: python function python-2.7 tkinter


【解决方案1】:

这是一个完整的示例,可以满足您的要求(我认为。)

创建了一个循环函数,您可以选择它的重复频率,然后您可以通过按下按钮将其终止(使用after_cancel,因此程序仍将运行,但不会执行任何操作。

from tkinter import *

root = Tk()

global counter
counter = 0

def ok():
    print('function: "ok" is running now.')
    global counter
    counter += 1
    def stop():
        global recur
        label.destroy()
        stop_button.destroy()
        root.after_cancel(recur)
    try:
        if counter == 1:
            global time
            time = int(entry.get())
            label.config(text = 'your only option now is to stop the function')
            entry.destroy()
            ok_button.destroy()
            stop_button = Button(text = 'stop', command = stop)
            stop_button.pack()
            global recur
            print ('function will start again in', time, 'seconds')
            recur = root.after(1000*time, ok)
        else:
            print ('function will start again in', time, 'seconds')
            recur = root.after(1000*time, ok)
    except ValueError:
        counter = 0
        print('thats not a number')

label = Label(root, text = 'pick a number of seconds for the function to recur in')
label.pack()

entry = Entry(root)
entry.pack()

ok_button = Button(root, text = 'Ok', command = ok)
ok_button.pack()

root.title('cool recurring function')
root.mainloop()

希望那是你想要的,如果不是大喊大叫(告诉我你想要什么)!

【讨论】:

  • 哦,是的,忘了提到 mgilson 是第一个给出正确评论的人,我只是想我会扩展它并给出一个工作示例。
猜你喜欢
  • 1970-01-01
  • 2019-06-23
  • 2011-01-18
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 2013-02-11
相关资源
最近更新 更多