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

该代码创建了一个TkinterGUI应用,用于列出并显示指定文件夹中具有特定扩展名(如.jpg和.png)的图片。应用包含两个按钮,一个用于列出图片文件名,另一个用于在文本组件中显示图片。图片被缩放以适应窗口大小。
摘要由CSDN通过智能技术生成
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)




要在 Python 中使用 Canvas 界面实现多张图片同一界面,你可以使用 Matplotlib 库。Matplotlib 是一个 Python 的绘图库,它可以生成各种类型的图形,包括线图、散点图、柱状图等等。 下面是一个简单的例子,演示如何在 Canvas 界面中显示多个 Matplotlib 图形: ```python import tkinter as tk from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure # 创建一个 tkinter 窗口 root = tk.Tk() root.title("Multiple Plots in Canvas") # 创建一个 Figure 对象 fig = Figure() # 在 Figure 中创建两个 Axes 对象 ax1 = fig.add_subplot(211) ax2 = fig.add_subplot(212) # 在两个 Axes 中绘制图形 ax1.plot([1, 2, 3], [4, 5, 6]) ax2.plot([3, 2, 1], [6, 5, 4]) # 创建两个 FigureCanvasTkAgg 对象 canvas1 = FigureCanvasTkAgg(fig, master=root) canvas2 = FigureCanvasTkAgg(fig, master=root) # 在 tkinter 窗口中放置两个 FigureCanvasTkAgg 对象 canvas1.get_tk_widget().grid(row=0, column=0) canvas2.get_tk_widget().grid(row=1, column=0) # 启动 tkinter 主循环 root.mainloop() ``` 在这个例子中,我们创建了一个 tkinter 窗口,并在其中创建了一个 Figure 对象。然后,我们在 Figure 中创建了两个 Axes 对象,并在其中绘制了图形。接着,我们创建了两个 FigureCanvasTkAgg 对象,并将它们放置在 tkinter 窗口中。最后,我们启动了 tkinter 的主循环。 当你运行这个例子时,你会看到两个 Matplotlib 图形显示在同一tkinter 窗口中。你可以根据自己的需求修改这个例子,以在同一界面中显示更多的图形。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值