简体   繁体   English

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

[英]how to show images in listbox in tkinter

i want to show images in the listbox tkinter widget.my code look like this.我想在列表框中显示图像 tkinter 小部件。我的代码如下所示。

code代码

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)

How to print this image in listbox?如何在列表框中打印此图像?

As someone mentioned in a comment you can't put images in a Listbox , but as I mentioned in another, you could use a Text widget instead because you can put images into them.正如评论中提到的那样,您不能将图像放入Listbox ,但正如我在另一个中提到的那样,您可以改用Text小部件,因为您可以将图像放入其中。 Below is a relatively simple demonstration of how something like that can be done.下面是一个相对简单的演示,说明如何完成类似的事情。 Its two buttons illustrate how it can display just text or a combination of the two simultaneously.它的两个按钮说明了它如何只显示文本或同时显示两者的组合。 Text widget are actually very versatile. Text小部件实际上用途广泛。

Anyway, since presumably you want a scrollable list, the Text subclass named tkinter.scrolledtext is used instead of plain one to save work.无论如何,因为大概你想要一个可滚动的列表,所以使用名为tkinter.scrolledtextText子类而不是普通的子类来节省工作。 Since it's a subclass, it can do anything its baseclass can do.由于它是一个子类,它可以做任何它的基类可以做的事情。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM