[英]How to get a photoimage object from a list using indexes
我使用 ImageTk 创建了一个 photoImage 对象列表,代码如下:
imageList = [#images]
photoImageList = []
for image in imageList:
photoImageList.append(ImageTk.PhotoImage(Image.open(image)))
我这样做是为了可以直接访问 photoImage 对象以将它们放入 label 中,而不必在我想做的时候重复该过程。 直接打印列表返回:
print(photoImageList)
>>> [<PIL.ImageTk.PhotoImage object at x>, ...]
打印列表中的单个 object 将返回:
print(photoImageList[0])
>>> pyimage1
每当我尝试访问程序中不同位置的单个图像时,如下所示:
label = Label(root, image=photoImageList[3])
它给了我以下错误:
_tkinter.TclError: image "pyimage1" doesn't exist
我将如何从列表中访问 photoImage object 而不是 pyimageX,这在 label 中是否有效?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.