Tkinter GIF 不显示
Tkinter GIF not display
我的 gif 是冻结的“-”(不是 displayed/animated,但可以在 tkinter 之外运行)。其他方法我都试过了,还是不行,代码:
import tkinter as tk
from tkinter import *
def animate(self):
global pic
pic = PhotoImage(file="/home/arnaldo/Desktop/b.gif")
print("called1")
self.canvas = Canvas(width=350, height=233, bg='white')
self.canvas.pack(expand=YES, fill=BOTH)
self.canvas.create_image((0, 0), image=pic, anchor=NW)
class run(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
animate(self)
run().mainloop()
Tkinter 不会自动显示动画 gif。您必须通过循环加载每一帧来自己制作动画。
见
如果您的问题是您根本看不到任何图像,可能是因为图像被垃圾收集了。
见Cannot Display an Image in Tkinter
我解决了我的问题,这是代码,你必须拆分 gif (http://gifmaker.me/exploder/),并将图像添加到文件夹 (gifBackgroundDirectory) 和 运行...:[=12 =]
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()
我的 gif 是冻结的“-”(不是 displayed/animated,但可以在 tkinter 之外运行)。其他方法我都试过了,还是不行,代码:
import tkinter as tk
from tkinter import *
def animate(self):
global pic
pic = PhotoImage(file="/home/arnaldo/Desktop/b.gif")
print("called1")
self.canvas = Canvas(width=350, height=233, bg='white')
self.canvas.pack(expand=YES, fill=BOTH)
self.canvas.create_image((0, 0), image=pic, anchor=NW)
class run(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
animate(self)
run().mainloop()
Tkinter 不会自动显示动画 gif。您必须通过循环加载每一帧来自己制作动画。
见
如果您的问题是您根本看不到任何图像,可能是因为图像被垃圾收集了。
见Cannot Display an Image in Tkinter
我解决了我的问题,这是代码,你必须拆分 gif (http://gifmaker.me/exploder/),并将图像添加到文件夹 (gifBackgroundDirectory) 和 运行...:[=12 =]
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()