【发布时间】:2015-04-15 14:02:55
【问题描述】:
我一直在尝试使用Tkinter.PhotoImage 播放动画 gif,但没有看到任何成功。它显示图像,但不显示动画。以下是我的代码:
root = Tkinter.Tk()
photo = Tkinter.PhotoImage(file = "path/to/image.gif")
label = Tkinter.Label(image = photo)
label.pack()
root.mainloop()
它在窗口中显示图像,仅此而已。我认为这个问题与Tkinter.Label 有关,但我不确定。我一直在寻找解决方案,但他们都告诉我使用 PIL(Python Imaging Library),这是我不想使用的东西。
有了答案,我又创建了一些代码(还是不行...),这里是:
from Tkinter import *
def run_animation():
while True:
try:
global photo
global frame
global label
photo = PhotoImage(
file = photo_path,
format = "gif - {}".format(frame)
)
label.configure(image = nextframe)
frame = frame + 1
except Exception:
frame = 1
break
root = Tk()
photo_path = "/users/zinedine/downloads/091.gif"
photo = PhotoImage(
file = photo_path,
)
label = Label(
image = photo
)
animate = Button(
root,
text = "animate",
command = run_animation
)
label.pack()
animate.pack()
root.mainloop()
感谢一切! :)
【问题讨论】:
-
您可以通过在
Canvas小部件 (C.create_image(x, y, image=photo) 上使用它来检查它是否与附加到Label小部件有关。 -
我不知道我是否对
Canvas做错了,但我只看到图像的右下角,而且看起来像素化了... -
尝试先使用非动画图像,然后再切换到动画图像。
-
非动画 gif 也会发生同样的事情...
-
没有看到代码我只能说你一定做错了。
标签: python animation tkinter gif