gpt4 book ai didi

python - tkinter 中的 GIF 故障

转载 作者:行者123 更新时间:2023-12-01 07:20:11 24 4
gpt4 key购买 nike

我创建了一个包含 gif 的 GUI 应用程序。然而,在工作过程中,gif 开始失败。

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

frames = []
for i in range(61):
frames.append(PhotoImage(file='bell.gif', format='gif -index ' + str(i)))

def update(ind):
try:
frame = frames[ind]
except IndexError:
ind = 1
frame = frames[ind]

ind += 1

label.configure(image=frame, bg="white")
label.pack()
root.after(100, update, ind)

label = Label(root, bg="white")

root.after(100, update(1))

root.mainloop()

这是原始的 gif: https://im4.ezgif.com/tmp/ezgif-4-e5132d340f75.gif

最后会发生什么: https://s3.gifyu.com/images/glitch9af7397783b31f35.gif

最佳答案

已更新您必须将 Pillow 纳入包装和作品中。

enter image description here

enter image description here

找到另一种方式

from tkinter import *
from PIL import Image, ImageTk

class MyLabel(Label):
def __init__(self, master, filename):
im = Image.open(filename)
seq = []
try:
while 1:
seq.append(im.copy())
im.seek(len(seq)) # skip to next frame
except EOFError:
pass # we're done

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

first = seq[0].convert('RGBA')
self.frames = [ImageTk.PhotoImage(first)]

Label.__init__(self, master, image=self.frames[0])

temp = seq[0]
for image in seq[1:]:
temp.paste(image)
frame = temp.convert('RGBA')
self.frames.append(ImageTk.PhotoImage(frame))

self.idx = 0

self.cancel = self.after(self.delay, self.play)

def play(self):
self.config(image=self.frames[self.idx])
self.idx += 1
if self.idx == len(self.frames):
self.idx = 0
self.cancel = self.after(self.delay, self.play)


root = Tk()
anim = MyLabel(root, 'bell.gif')
anim.pack()
root.mainloop()

关于python - tkinter 中的 GIF 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com