0

The standard way to end ontimer() in Python Turtle Graphics is to simply not repeat calling it, based on some condition.

In my use case however, I need to actively kill the timer.

The relevent part of code from the turtle module is:

    def _ontimer(self, fun, t):
        """Install a timer, which calls fun after t milliseconds.
        """
        if t == 0:
            self.cv.after_idle(fun)
        else:
            self.cv.after(t, fun)

This answer explains how to destroy a timer using pure python tkinter. I found the after method in turtle._Root but I can't see how to get access to the id for it, or figure out exactly how I would use it if I had it.

Any help much appredciated.

Here's a sample program I would like to apply the solution to:

import turtle

turtle.shape("turtle")

def move():
    turtle.fd(20)
    turtle.ontimer(move, 500)

move()

turtle.done()
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
  • Does this answer your question? [Tkinter after\_cancel in python](https://stackoverflow.com/questions/25702094/tkinter-after-cancel-in-python). Avoid the term `kill`, read [Is there any way to kill a Thread?](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) – stovfl Mar 15 '20 at 12:55
  • As according to the code in `turtle` module, `ontimer()` is actually using `tkinter.after` function internally. So you can directly use `after_id = turtle.getcanvas().after(500, move)` instead of `turtle.ontimer(...)` and then use `.after_cancel(after_id)` to cancel the timer task. – acw1668 Mar 16 '20 at 03:41

0 Answers0