首页 > 解决方案 > Tkinter 动画

问题描述

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()

为什么标签...“标签”没有更新为当前帧,而是显示为默认标签(灰色)?

标签: pythontkinter

解决方案


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()

推荐阅读