0
num = 0
def animate():
    global num
    print(num)
    img = PhotoImage(file = "gif.gif", format = "gif -index {}".format(100))
    label.configure(image = img)
    num = (num+1)%180
    screen.after(25, animate)
animate()

Why the Label... "label" is not updated as the current frame, instead just appears as a default label(gray color)?

YarinB
  • 73
  • 1
  • 8
  • No, I did save the photo in a variable and it still does not work for me – YarinB Jan 26 '21 at 10:19
  • Saving the photo to a local variable does not work. Either save it to a global variable or an attribute of the label. – acw1668 Jan 26 '21 at 10:38

2 Answers2

0

Try saving the image as a global variable, like so

num = 0
img = None
def animate():
    global num
    global img
    print(num)
    img = PhotoImage(file = "gif.gif", format = "gif -index {}".format(num))
    label.configure(image = img)
    num = (num+1)%180
    screen.after(25, animate)
animate()
YarinB
  • 73
  • 1
  • 8
oskros
  • 3,101
  • 2
  • 9
  • 28
  • Excellent! However I realized the program gets slower as far as the frame is higher on index, Do you know why is it so? – YarinB Jan 26 '21 at 10:32
  • Cant tell you sorry. You can try to load all the frames before starting the animation instead, maybe that'll improve the speed. See an example here: https://stackoverflow.com/questions/28518072/play-animations-in-gif-with-tkinter – oskros Jan 26 '21 at 10:40
  • I tried loading every frame into a single list, but this proccess was way to long - about 9 seconds - just to finish loading every single frame to the list. thanks for your help anyways – YarinB Jan 26 '21 at 10:49
0

Better use Pillow module to handle the GIF frames:

from PIL import Image, ImageTk

...

image = Image.open("gif.gif") # load the image

def animate(num=0):
    num %= image.n_frames
    image.seek(num) # seek to the required frame
    img = ImageTk.PhotoImage(image)
    label.config(image=img)
    label.image = img # save a reference of the image
    label.after(25, animate, num+1)

animate()
acw1668
  • 40,144
  • 5
  • 22
  • 34