创建tkinter中的图像列表



我有一个工作代码,其中tkinter应用程序获取GIF文件并在单击按钮时显示它对于单个GIF图像,它工作得很好。

我想要的是有一个额外的按钮下一个图像,当我点击它,它应该显示下一个图像。如何创建图像列表?

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)
self.button = Button(text="Zoom out",command=self.play)
self.button.place(relx=0.4, rely=0.5, anchor=CENTER)

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()

def stop_it():
anim.after_cancel(anim.cancel)
anim = MyLabel(root,'A.gif')
anim.pack()
stop_it()
root.mainloop()

我的想法是如果我可以使用下面的代码:

images = ['A.gif','B.gif']
images = iter(images)
img = next(images)

但是我如何在我当前的代码中实现这个呢?

可能有很多方法来制作图像列表,但我想让它像这样:


# images 
img1 = ImageTk.PhotoImage(Image.open("a.gif"))
img2 = ImageTk.PhotoImage(Image.open("b.gif"))
img3 = ImageTk.PhotoImage(Image.open("c.gif"))
# a list of images 
Image_list = [img1 , img2 , img3 ]

对于迭代,我会创建一个计数器


counter = len(Image_list)
img_panel = Label(root , image = Image_list[counter] ) 
img_panel.pack()
def next_image():
counter-=1
img_panel = Label(root , image = Image_list[counter] ) 
img_panel.pack()
next_image = Button(root , text= "next image" , command =next_image)
next_image.pack()

相关内容

  • 没有找到相关文章

最新更新