11

I'm having a problem stopping the 'feed'; the cancel argument doesn't seem to have any impact on the after method. Although "feed stopped" is printed to the console.

I'm attempting to have one button that will start the feed and another that will stop the feed.

from Tkinter import Tk, Button
import random

    def goodbye_world():
        print "Stopping Feed"
        button.configure(text = "Start Feed", command=hello_world)
        print_sleep(True)

    def hello_world():
        print "Starting Feed"
        button.configure(text = "Stop Feed", command=goodbye_world)
        print_sleep()

    def print_sleep(cancel=False):
        if cancel==False:
            foo = random.randint(4000,7500)
            print "Sleeping", foo
            root.after(foo,print_sleep)
        else:
            print "Feed Stopped"


    root = Tk()
    button = Button(root, text="Start Feed", command=hello_world)

    button.pack()


    root.mainloop()

With the output:

Starting Feed
Sleeping 4195
Sleeping 4634
Sleeping 6591
Sleeping 7074
Stopping Feed
Sleeping 4908
Feed Stopped
Sleeping 6892
Sleeping 5605
Sheldon
  • 9,639
  • 20
  • 59
  • 96
  • I'm not entirely sure about how tkinter works, but this looks quite like a threading issue to me. If you make cancel a (thread safe) global, does that resolve the issue? – mklauber Mar 19 '12 at 19:46
  • @mklauber: no, not a threading issue. Tkinter is single-threaded. – Bryan Oakley Mar 19 '12 at 20:31
  • @BryanOakley: Thanks. This is why I posted as a comment. I wasn't sure so I just wanted to raise the question. – mklauber Mar 19 '12 at 20:33

3 Answers3

28

The problem is that, even though you're calling print_sleep with True to stop the cycle, there's already a pending job waiting to fire. Pressing the stop button won't cause a new job to fire but the old job is still there, and when it calls itself, it passes in False which causes the loop to continue.

You need to cancel the pending job so that it doesn't run. For example:

def cancel():
    if self._job is not None:
        root.after_cancel(self._job)
        self._job = None

def goodbye_world():
    print "Stopping Feed"
    cancel()
    button.configure(text = "Start Feed", command=hello_world)

def hello_world():
    print "Starting Feed"
    button.configure(text = "Stop Feed", command=goodbye_world)
    print_sleep()

def print_sleep():
    foo = random.randint(4000,7500)
    print "Sleeping", foo
    self._job = root.after(foo,print_sleep)

Note: make sure you initialize self._job somewhere, such as in the constructor of your application object.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    I need to do this but I need more detail - could you please provide a full example, including how to "initialize self._job somewhere, such as in the constructor of your application object."? – Robin Andrews Mar 15 '20 at 08:54
  • How to get the id of `root.after()`? to call `root.after_cancel()` – Delrius Euphoria Sep 07 '20 at 17:04
24

When you call root.after(...), it will return an identifier. You should keep track of that identifier (e.g., store it in an instance variable), and then you can later call root.after_cancel(after_id) to cancel it.

Edward Loper
  • 15,374
  • 7
  • 43
  • 52
2

Here is my answer with only 3 lines of code added. The answer lies in using .after_cancel(x) which in simple words, mean that "stop doing 'x' job". I believe in readability of code so I made only minimal changes to your code which did the job. Please have a look. Thanks.

from tkinter import Tk, Button
import random

keep_feeding = None


def goodbye_world():
    print("Stopping Feed")
    button.configure(text="Start Feed", command=hello_world)
    print_sleep(True)


def hello_world():
    print("Starting Feed")
    button.configure(text="Stop Feed", command=goodbye_world)
    print_sleep()


def print_sleep(cancel=False):
    global keep_feeding
    if not cancel:
        foo = random.randint(1000, 2500)
        print(f"Sleeping {foo}")
        keep_feeding = root.after(foo, print_sleep)
    else:
        root.after_cancel(keep_feeding)
        print("Feed Stopped")


root = Tk()
button = Button(root, text="Start Feed", command=hello_world)

button.pack()

root.mainloop()
cursorrux
  • 1,382
  • 4
  • 9
  • 20
Vaidik
  • 21
  • 5