使用 tkinter 在 python 中播放动画 GIF [英] Play an Animated GIF in python with tkinter

查看:83
本文介绍了使用 tkinter 在 python 中播放动画 GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python3 和 tkinter 创建一个虚拟宠物风格的游戏.到目前为止,我已经有了主窗口并开始放置标签,但我遇到的问题是播放动画 gif.我在这里搜索并找到了一些答案,但他们不断抛出错误.我发现的结果是使用 PhotoImage 的 gif 的索引位置在一定范围内继续.

I am wanting to create a virtual pet style game using python3 and tkinter. So far I have the main window and have started putting labels in, but the issue I am having is playing an animated gif. I have searched on here and have found some answers, but they keep throwing errors. The result I found has the index position of the gif using PhotoImage continue through a certain range.

    # Loop through the index of the animated gif
frame2 = [PhotoImage(file='images/ball-1.gif', format = 'gif -index %i' %i) for i in range(100)]

def update(ind):

    frame = frame2[ind]
    ind += 1
    img.configure(image=frame)
    ms.after(100, update, ind)

img = Label(ms)
img.place(x=250, y=250, anchor="center")

ms.after(0, update, 0)
ms.mainloop()

当我在终端中使用pyhton3 main.py"运行它时,出现以下错误:

When I run this in terminal with "pyhton3 main.py" I get the following error:

_tkinter.TclError: 此索引没有图像数据

_tkinter.TclError: no image data for this index

我忽略或完全遗漏了什么?

What am I overlooking or completely leaving out?

这里是 GitHub 存储库的链接以查看完整项目:VirtPet_Python

Here is the link to the GitHub repository to see the full project:VirtPet_Python

提前致谢!

推荐答案

该错误表示您尝试加载 100 帧,但 gif 少于该值.

The error means that you tried to load 100 frames, but the gif has less than that.

tkinter 中的动画 gif 是出了名的糟糕.我很久以前写过这段代码,你可以从中窃取,但是除了小 gif 之外的任何东西都会变得迟钝:

Animated gifs in tkinter are notoriously bad. I wrote this code an age ago that you can steal from, but will get laggy with anything but small gifs:

import tkinter as tk
from PIL import Image, ImageTk
from itertools import count

class ImageLabel(tk.Label):
    """a label that displays images, and plays them if they are gifs"""
    def load(self, im):
        if isinstance(im, str):
            im = Image.open(im)
        self.loc = 0
        self.frames = []

        try:
            for i in count(1):
                self.frames.append(ImageTk.PhotoImage(im.copy()))
                im.seek(i)
        except EOFError:
            pass

        try:
            self.delay = im.info['duration']
        except:
            self.delay = 100

        if len(self.frames) == 1:
            self.config(image=self.frames[0])
        else:
            self.next_frame()

    def unload(self):
        self.config(image="")
        self.frames = None

    def next_frame(self):
        if self.frames:
            self.loc += 1
            self.loc %= len(self.frames)
            self.config(image=self.frames[self.loc])
            self.after(self.delay, self.next_frame)

root = tk.Tk()
lbl = ImageLabel(root)
lbl.pack()
lbl.load('ball-1.gif')
root.mainloop()

这篇关于使用 tkinter 在 python 中播放动画 GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆