tkinter 同一界面滚动展示多张图片

该代码创建了一个TkinterGUI应用,用于列出并显示指定文件夹中具有特定扩展名(如.jpg和.png)的图片。应用包含两个按钮,一个用于列出图片文件名,另一个用于在文本组件中显示图片。图片被缩放以适应窗口大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import tkinter
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.root.geometry('600x500')
        self.image_folder_path = image_folder_path
        self.image_file_extensions = image_file_extensions
        # 点击按钮展示图片
        self.create_widgets()
        # 直接展示图片
        # self.show_image()
        tkinter.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',width=10 , 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()
        for i in range(10):
            for filepath in Path(self.image_folder_path).iterdir():
                if filepath.suffix in self.image_file_extensions:
                    self.text.image_filenames.append(filepath)

        # Display images in Text widget.
        for image_file_path in self.text.image_filenames:
            img = Image.open(image_file_path)
            # img.resize((200, 200), 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')

    def show_image(self):
        frame = tkinter.Frame(self.root)
        frame.place(relwidth=1, relheight=1, width=600, height=400)
        screenwidth = frame.winfo_screenmmwidth()
        screenheight = frame.winfo_screenmmheight()
        self.text = ScrolledText(frame, wrap=WORD)
        self.text.place(x=1, y=50, width=600, height=400)
        self.text.image_filenames = []
        self.text.images = []
        self.text.delete('1.0', END)
        self.text.images.clear()

        # 展示多张图片
        # 存储图片路径
        for i in range(10):
            for filepath in Path(self.image_folder_path).iterdir():
                print(filepath)
                print(filepath.suffix)
                if filepath.suffix in self.image_file_extensions:
                    self.text.image_filenames.append(filepath)
        # 存储图片路径
        # self.text.image_filenames = ['./Image/setup1.png', './Image/setup2.png']
        
        for image_file_path in self.text.image_filenames:
            img = Image.open(image_file_path)
            # 缩放图片
            img.resize((int(screenwidth*0.8), int(screenheight*0.8)), 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) 
            self.text.insert(INSERT, '\n')


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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值