quantik
quantik

Reputation: 816

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)):
        label.configure(image=frames[n])
        n+=1
        root.after(300, animate(n))

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

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

Upvotes: 0

Views: 126

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

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.

Upvotes: 2

Related Questions