我一直在尝试使用
Tkinter.PhotoImage
播放一个动画gif,但一直没有成功。它会显示图像,但不会播放动画。以下是我的代码:
我一直在尝试使用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
组件的附加有关。 - martineau