python - 如何在 tkinter 的列表框中显示图像?

标签 python tkinter

我想在 Listbox tkinter 小部件中显示图像。我的代码如下所示:

mylist = Listbox(self.labelFrame3, yscrollcommand=self.yscrollbar.set, selectmode=SINGLE)
mylist.grid(row=0, column=0, rowspan=21, columnspan=21, sticky=(N, S, E, W))

img = Image.open('modeldata/photosamples/amanullahattamuhammad2.0.jpg')  # PIL solution
img = img.resize((200, 200), Image.ANTIALIAS)  # The (250, 250) is (height, width)
img = ImageTk.PhotoImage(img)

如何在Listbox中打印此图像?

最佳答案

正如有人在评论中提到的,您不能将图像放入 Listbox 中,但正如我在另一篇文章中提到的,您可以使用 Text 小部件,因为您可以将图像放入其中。下面是一个相对简单的演示,展示了如何完成类似的事情。它的两个按钮说明了它如何仅显示文本或同时显示两者的组合。 Text 小部件实际上非常通用。

无论如何,由于您可能想要一个可滚动列表,因此名为 tkinter.scrolledtextText 子类使用它代替普通的以节省工作。由于它是一个子类,因此它可以执行其基类可以执行的任何操作。

from pathlib import Path
from PIL import Image, ImageTk
import tkinter as tk
from tkinter.constants import *
from tkinter.scrolledtext import ScrolledText


class App:
    def __init__(self, image_folder_path, image_file_extensions):
        self.root = tk.Tk()
        self.image_folder_path = image_folder_path
        self.image_file_extensions = image_file_extensions
        self.create_widgets()
        self.root.mainloop()

    def create_widgets(self):
        self.list_btn = tk.Button(self.root, text='List Images', command=self.list_images)
        self.list_btn.grid(row=0, column=0)
        self.show_btn = tk.Button(self.root, text='Show Images', command=self.show_images)
        self.show_btn.grid(row=1, column=0)

        self.text = ScrolledText(self.root, wrap=WORD)
        self.text.grid(row=2, column=0, padx=10, pady=10)

        self.text.image_filenames = []
        self.text.images = []

    def list_images(self):
        ''' Create and display a list of the images the in folder that have one
            of the specified extensions. '''
        self.text.image_filenames.clear()
        for filepath in Path(self.image_folder_path).iterdir():
            if filepath.suffix in self.image_file_extensions:
                self.text.insert(INSERT, filepath.name+'\n')
                self.text.image_filenames.append(filepath)

    def show_images(self):
        ''' Show the listed image names along with the images themselves. '''
        self.text.delete('1.0', END)  # Clear current contents.
        self.text.images.clear()
        # Display images in Text widget.
        for image_file_path in self.text.image_filenames:
            img = Image.open(image_file_path).resize((64, 64), Image.ANTIALIAS)
            img = ImageTk.PhotoImage(img)

            self.text.insert(INSERT, image_file_path.name+'\n')
            self.text.image_create(INSERT, padx=5, pady=5, image=img)
            self.text.images.append(img)  # Keep a reference.
            self.text.insert(INSERT, '\n')


image_folder_path = 'modeldata/photosamples'
image_file_extensions = {'.jpg', '.png'}
App(image_folder_path, image_file_extensions)

这是在我的计算机上的一个文件夹上运行的,并显示了图像:

screenshot

关于python - 如何在 tkinter 的列表框中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66622157/

相关文章:

python - 如何打印一个文件中不包含另一个文件中任何字符串的行

python - numpy 乘法表输出

python - 在 Python 中从 URL 下载文本

python - Python AttributeError : I don't know why this is not working [closed]

python - TKInter - 标签中的文本改变窗口的宽度

python - 重新运行程序时出现问题

python - 比较两个不同文件中的字符串的脚本

python - 如何从 tkinter 复选框中删除勾号

python - 在python中使用tkinter从顶层窗口关闭主窗口

python - tkinter/py2app 创建的应用程序在初始启动时不显示窗口