新闻组:comp.lang.python的
来源: “弗雷德里克Lundh开发”
日期:星期一,2006年
丹尼尔Nogradi写道5月1日:
“的源代码发布1.1.4版本的脚本中包含脚本 目录,您可以在其中找到player.py,gifmaker.py和explode.py ,这些都涉及动画gif。
他们仍然装运1.1.5(和1.1.6),他们应该工作。
如果你错过是从脚本目录中的几个文件,你可以得到 他们在这里:
http://svn.effbot.org/public/pil/Scripts/
player.py从从命令行运行
看看这一个是否适合你:
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()
谢谢,但我是一个双对Python新手,我该如何真正使用脚本?我已经导入了tkinter,PIL和player,并且已经打开了图像,我尝试了'animation = player.UI(root,im)'和一些变体。播放器脚本中的UI类是否为标签?当我尝试打包()它时,它说“UI对象没有属性tk”,如果我不包装()或网格(),则什么都不会发生。 –