Newsgroups: comp.lang.python
“Fredrik Lundh”
日期:莫 2006年5月1日
Daniel Nogradi写道:
The source distribution of the 1.1.4 version comes with a Scripts
directory where you can find player.py, gifmaker.py and explode.py
which all deal with animated gif.
他们仍用1.1.5(和1.1.6)重新运输,应当工作。
if all you re missing is a few files from the script directory, you can get
them here:
http://svn.effbot.org/public/pil/casts/
参与者来自指挥线。
see if this one works for you:
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, animated.gif )
anim.pack()
def stop_it():
anim.after_cancel(anim.cancel)
Button(root, text= stop , command=stop_it).pack()
root.mainloop()