该错误表示您尝试加载100帧,但是gif小于此数量。
众所周知,tkinter中的gif动画效果很差。我在很久以前就编写了这段代码,您可以从中窃取,但是除了小gif之外,其他任何东西都不会让您感到la懒:
import tkinter as tkfrom PIL import Image, ImageTkfrom itertools import countclass 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()


