Tkinter GIF无法显示

时间:2015-09-15 19:35:37

标签: python python-3.x tkinter gif

我的gif已冻结' - '(未显示/动画,但在tkinter之外)。我在其他所有方面都努力了,但仍然无法工作,代码:

open()

2 个答案:

答案 0 :(得分:2)

Tkinter没有自动显示GIF动画。您必须通过在循环中加载每个帧来自己动画。

请参阅Play Animations in GIF with Tkinter

如果您的问题是您根本没有看到任何图片,可能是因为图片被垃圾收集了。

请参阅Cannot Display an Image in Tkinter

答案 1 :(得分:0)

我解决了我的问题,这是代码,您必须拆分gif(http://gifmaker.me/exploder/),并将图像添加到文件夹(gifBackgroundDirectory)并运行...:

import tkinter as tk
from tkinter import Canvas, PhotoImage
import os

#YOUR IMAGE FOLDER, THAT CONTAINS tmp-0.gif, tmp-1.gif
gifBackgroundDirectory = "/gif/"

def animate(self):

    #CHECK IF LIST IS EMPTY
    if len(self.gifBackgroundImages) == 0:
        #CREATE FILES IN LIST
        for foldername in os.listdir(gifBackgroundDirectory):
            self.gifBackgroundImages.append(foldername)
        #ALPHABETICAL ORDER
        self.gifBackgroundImages.sort(key = lambda x: int(x.split('.')[0].split('-')[1]))

    if self.atualGifBackgroundImage == len(self.gifBackgroundImages):
        self.atualGifBackgroundImage = 0

    self.background["file"] = gifBackgroundDirectory + self.gifBackgroundImages[self.atualGifBackgroundImage]
    self.label1["image"] = self.background
    self.atualGifBackgroundImage += 1

    #MILISECONDS\/ PER FRAME
    self.after(100, lambda: animate(self))

class run(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        self.label1 = tk.Label(self)
        self.label1.pack()

        #SET ANIMATEDBACKGROUND
        self.gifBackgroundImages = list()
        self.atualGifBackgroundImage = 0
        self.background = tk.PhotoImage()
        animate(self)

run().mainloop()