simple animation using tkinter simple animation using tkinter tkinter tkinter

simple animation using tkinter


The basic pattern for doing animation or periodic tasks with Tkinter is to write a function that draws a single frame or performs a single task. Then, use something like this to call it at regular intervals:

def animate(self):    self.draw_one_frame()    self.after(100, self.animate)

Once you call this function once, it will continue to draw frames at a rate of ten per second -- once every 100 milliseconds. You can modify the code to check for a flag if you want to be able to stop the animation once it has started. For example:

def animate(self):    if not self.should_stop:        self.draw_one_frame()        self.after(100, self.animate)

You would then have a button that, when clicked, sets self.should_stop to False


I just wanted to add Bryan's answer. I don't have enough rep to comment.

Another idea would be to use self.after_cancel() to stop the animation.

So...

def animate(self):    self.draw_one_frame()    self.stop_id = self.after(100, self.animate)def cancel(self):    self.after_cancel(self.stop_id)