1

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.

sirvar
  • 308
  • 6
  • 22
  • 3
    don't use `time.sleep` ... You'll kill your app. use `tkinter.Widget.after`. See e.g. http://stackoverflow.com/a/12043756/748858 and http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method – mgilson Oct 01 '14 at 23:50

1 Answers1

1

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

W1ll1amvl
  • 1,219
  • 2
  • 16
  • 30
  • oh yeah forgot to mention that mgilson was first to give a correct comment, i just thought i would expand on it and give a working example. – W1ll1amvl Oct 02 '14 at 02:31