0

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?

Community
  • 1
  • 1
quantik
  • 776
  • 12
  • 26

1 Answers1

2

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • This works! I guess I didn't fully understand the `after` documentation I thought it would wait for the `300` milliseconds then call `animate(args)`. Thanks! – quantik May 08 '17 at 21:26