quantik quantik - 1 year ago 60
Python Question

Tkinter failing to animate gif

I'm using tkinter to animate a gif using guidance from the following pages:
Tkinter animation will not work

However I cannot get the suggestions to work. Right now I have something like this

import tkinter as tk
root = tk.Tk()
frames = [tk.PhotoImage(file = '/home/quantik/Pictures/dice.gif',format="gif -index %i" %(i)) for i in range(5)]
def animate(n):
if (n < len(frames)):
root.after(300, animate(n))

label = tk.Label(root)
root.after(0, animate(0))

However it just displays the last image in
I've tried several methods but I keep getting the same result. Does anyone have any suggestions as to why this may be happening?

Answer Source

This code:

root.after(300, animate(n))

is exactly the same as this code:

result = animate(n)
root.after(300, result)

Notice what is happening? You don't want to call the function, you want to tell after to call it later. You do that like this:

root.after(300, animate, n)

That tells root.after to call the animate function after 300 milliseconds, and to pass the value of n as an argument to the function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download