真是难以置信。。。所有人都提出了一些特殊的包来播放动画GIF,目前,它可以与Tkinter和经典的PIL模块!
这是我自己的GIF动画方法(我刚才创建的)。非常简单:
from Tkinter import *
from PIL import Image, ImageTk
from time import sleep
def stop(event):
global play
play = False
exit()
root = Tk()
root.bind("<Key>", stop) # Press any key to stop
GIFfile = {path_to_your_GIF_file}
im = Image.open(GIFfile); img = ImageTk.PhotoImage(im)
delay = float(im.info['duration'])/1000; # Delay used in the GIF file
lbl = Label(image=img); lbl.pack() # Create a label where to display images
play = True; frame = 0
while play:
sleep(delay);
frame += 1
try:
im.seek(frame); img = ImageTk.PhotoImage(im)
lbl.config(image=img); root.update() # Show the new frame/image
except EOFError:
frame = 0 # Restart
root.mainloop()
注意:我不确定连续的帧是从内存还是从文件(磁盘)中读取的。在第二种情况下,如果它们都同时读取并保存到一个数组(列表)中,则效率会更高。(我不太想知道!:)