Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple animation using tkinter

I have a simple code to visualise some data using tkinter. A button click is bound to the function that redraws the next "frame" of data. However, I'd like to have the option to redraw automatically with a certain frequency. I'm very green when it comes to GUI programming (I don't have to do a lot for this code), so most of my tkinter knowledge comes from following and modifying examples. I guess I can use root.after to achieve this, but I'm not quite sure I understand how from other codes. The basic structure of my program is as follows:

# class for simulation data
# --------------------------------

def Visualisation:

   def __init__(self, args):
       # sets up the object


   def update_canvas(self, Event):
       # draws the next frame

       canvas.delete(ALL)

       # draw some stuff
       canvas.create_........


# gui section
# ---------------------------------------

# initialise the visualisation object
vis = Visualisation(s, canvasWidth, canvasHeight)

# Tkinter initialisation
root = Tk()
canvas = Canvas(root, width = canvasWidth, height = canvasHeight)

# set mouse click to advance the simulation
canvas.grid(column=0, row=0, sticky=(N, W, E, S))
canvas.bind('<Button-1>', vis.update_canvas)

# run the main loop
root.mainloop()

Apologies for asking a question which I'm sure has an obvious and simple answer. Many thanks.

like image 812
stuwilmur Avatar asked Dec 12 '22 00:12

stuwilmur


2 Answers

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

like image 51
Bryan Oakley Avatar answered Dec 31 '22 04:12

Bryan Oakley


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)
like image 23
Ioannis Georgantas Avatar answered Dec 31 '22 04:12

Ioannis Georgantas