【发布时间】:2021-08-19 11:01:49
【问题描述】:
我希望 python 迭代目录中的图像并将其打包到 GUI
from tkinter import *
from pathlib import Path
from PIL import ImageTk, Image
root = Tk()
images = []
paths = Path("images").glob('**/*.png')
for path in paths:
photo = Image.open(path)
photo.thumbnail((100, 100))
img = ImageTk.PhotoImage(photo)
label = Label(image=img)
images.append(label)
for image in images:
image.pack()
root.mainloop()
但输出的只是目录中的最后一张图片:
【问题讨论】:
-
保留所有
img对象的列表。 -
这个问题和这个问题本质上是一样的:Why does Tkinter image not show up if created in a function?
标签: python tkinter python-imaging-library