Python 使用 Tkinter 播放 GIF 动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28518072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:21:59  来源:igfitidea点击:

Play Animations in GIF with Tkinter

pythonanimationtkintergif

提问by Zizouz212

I've been trying to play an animated gif using Tkinter.PhotoImage, but haven't been seeing any success. It displays the image, but not the animation. The following is my code:

我一直在尝试使用 播放动画 gif Tkinter.PhotoImage,但没有看到任何成功。它显示图像,但不显示动画。以下是我的代码:

root = Tkinter.Tk()
photo = Tkinter.PhotoImage(file = "path/to/image.gif")
label = Tkinter.Label(image = photo)
label.pack()
root.mainloop()

It displays the image in a window, and that's it. I'm thinking that the issue has something to do with Tkinter.Labelbut I'm not sure. I've looked for solutions but they all tell me to use PIL (Python Imaging Library), and it's something that I don't want to use.

它在一个窗口中显示图像,就是这样。我认为这个问题与此有关,Tkinter.Label但我不确定。我一直在寻找解决方案,但他们都告诉我使用 PIL(Python 成像库),而我不想使用它。

With the answer, I created some more code (which still doesn't work...), here it is:

有了答案,我创建了更多代码(仍然不起作用......),这里是:

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

Thanks for everything! :)

谢谢你的一切!:)

采纳答案by patthoyts

You have to drive the animation yourself in Tk. An animated gif consists of a number of frames in a single file. Tk loads the first frame but you can specify different frames by passing an index parameter when creating the image. For example:

您必须在 Tk 中自己驱动动画。动画 gif 由单个文件中的多个帧组成。Tk 加载第一帧,但您可以通过在创建图像时传递索引参数来指定不同的帧。例如:

frame2 = PhotoImage(file=imagefilename, format="gif -index 2")

If you load up all the frames into separate PhotoImages and then use timer events to switch the frame being shown (label.configure(image=nextframe)). The delay on the timer lets you control the animation speed. There is nothing provided to give you the number of frames in the image other than it failing to create a frame once you exceed the frame count.

如果您将所有帧加载到单独的 PhotoImages 中,然后使用计时器事件切换正在显示的帧 ( label.configure(image=nextframe))。计时器的延迟可让您控制动画速度。除了在超过帧数后无法创建帧之外,没有提供任何内容来为您提供图像中的帧数。

See the photoTk manual page for the official word.

官方词见photoTk 手册页。

回答by Alex

Here's a simpler example without creating an object:

这是一个不创建对象的简单示例:

from tkinter import *
import time
import os
root = Tk()

frames = [PhotoImage(file='mygif.gif',format = 'gif -index %i' %(i)) for i in range(100)]

def update(ind):

    frame = frames[ind]
    ind += 1
    label.configure(image=frame)
    root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()