【问题标题】:Python pack() function in for loops doesnt seem to wofor 循环中的 Python pack() 函数似乎没有
【发布时间】: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()

但输出的只是目录中的最后一张图片:

【问题讨论】:

标签: python tkinter python-imaging-library


【解决方案1】:

这是我的尝试:


from tkinter import *
from pathlib import Path
from PIL import ImageTk, Image

pippo = Tk()

images = []

paths = Path("images").glob('**/*.png')
for path in paths:
    print(path)
    photo = Image.open(path)
    photo.thumbnail((100, 100))
    img = ImageTk.PhotoImage(photo)
    images.append(img)

print(images)

for img in images:
    label = Label(pippo, image=img)
    label.pack(side = 'bottom')


pippo.mainloop()

从三个彩色圆圈开始的输出是:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2021-01-26
    • 1970-01-01
    • 2011-10-11
    • 2023-01-05
    • 1970-01-01
    • 2020-09-16
    相关资源
    最近更新 更多