简体   繁体   中英

call function after certain time and kill on button press, tkinter

I have made an entry widget and it gets the number entered when they click OK (this becomes the number of seconds for function to recur(n)), I want to call the function every n seconds. When OK is clicked, I want a button to show on the screen that will stop the function from recurring. How can I do this?

This is what I have:

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)

This is what I have for the stop timer button:

def stopTimer():
    global stop
    stop = 0
    stop_timer.grid_forget()

Thanks edit:

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")

I tried what you said but still not working. The color only changes once, then stops. Also, I would like the stoptimer button to change background with the background but it doesn't work. Thanks for your help.

Here is a complete example which does what you want it to (i think.)

a recurring function is created and you choose how often it recurs, then you can kill it with a button press (using after_cancel so program will still run, but wont do anything.).

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()

hope that was what you wanted, if not yell out (and tell me what you did want)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM