pyqt5按选择顺序多选图并显示缩略图

为了实现用户在选择图片时可以看到缩略图,并且按点击顺序显示最终的选择,我们可以自定义一个对话框,展示所有可选图片的缩略图,用户点击图片后会将其记录到列表中,并最终按顺序显示选择的缩略图。

解决方案思路:

  1. 自定义选择对话框:展示所有图片的缩略图,用户可以点击选择图片。
  2. 按点击顺序记录选择:记录用户点击图片的顺序。
  3. 显示选择结果:在主窗口中按点击顺序显示最终选择的图片缩略图。

实现步骤:

  1. 创建一个自定义对话框,加载文件夹内的图片,并显示为缩略图。
  2. 用户点击缩略图时,记录下点击的顺序。
  3. 用户确认选择后,按顺序在主窗口中显示缩略图。

完整示例代码:

python 复制代码
import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QScrollArea, QGridLayout, QFileDialog, QDialog, QHBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt

class ThumbnailDialog(QDialog):
    def __init__(self, folder_path, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Select Images")
        self.selected_files = []

        # 创建主布局
        self.layout = QVBoxLayout()
        self.grid_layout = QGridLayout()
        self.layout.addLayout(self.grid_layout)

        # 加载文件夹中的所有图片并显示为缩略图
        self.load_images(folder_path)

        # 确认按钮
        self.confirm_button = QPushButton('Confirm Selection')
        self.confirm_button.clicked.connect(self.accept)
        self.layout.addWidget(self.confirm_button)

        self.setLayout(self.layout)

    def load_images(self, folder_path):
        images = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
        row, col = 0, 0

        for image_file in images:
            image_path = os.path.join(folder_path, image_file)
            pixmap = QPixmap(image_path).scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation)

            label = QLabel()
            label.setPixmap(pixmap)
            label.setAlignment(Qt.AlignCenter)
            label.mousePressEvent = lambda event, path=image_path: self.select_image(path)

            self.grid_layout.addWidget(label, row, col)

            col += 1
            if col > 3:  # 每行4个缩略图
                col = 0
                row += 1

    def select_image(self, image_path):
        if image_path not in self.selected_files:
            self.selected_files.append(image_path)
            print(f"Selected: {image_path}")

    def exec_(self):
        super().exec_()
        return self.selected_files

class ImageSelector(QWidget):
    def __init__(self):
        super().__init__()

        # 设置窗口标题和大小
        self.setWindowTitle("Image Selector")
        self.setGeometry(100, 100, 800, 600)

        # 创建布局
        self.layout = QVBoxLayout()

        # 创建一个按钮用于选择图片
        self.select_button = QPushButton('Select Images')
        self.select_button.clicked.connect(self.open_folder_dialog)
        self.layout.addWidget(self.select_button)

        # 创建一个滚动区域来显示图片
        self.scroll_area = QScrollArea()
        self.scroll_area_widget = QWidget()
        self.grid_layout = QGridLayout(self.scroll_area_widget)

        self.scroll_area.setWidgetResizable(True)
        self.scroll_area.setWidget(self.scroll_area_widget)
        self.layout.addWidget(self.scroll_area)

        # 设置主布局
        self.setLayout(self.layout)

    def open_folder_dialog(self):
        # 打开文件夹对话框,用户选择一个文件夹
        folder_path = QFileDialog.getExistingDirectory(self, "Select Folder")

        if folder_path:
            # 打开自定义对话框,显示缩略图
            dialog = ThumbnailDialog(folder_path, self)
            selected_files = dialog.exec_()

            if selected_files:
                self.display_images(selected_files)

    def display_images(self, files):
        # 清除之前的布局内容
        for i in reversed(range(self.grid_layout.count())):
            widget_to_remove = self.grid_layout.itemAt(i).widget()
            self.grid_layout.removeWidget(widget_to_remove)
            widget_to_remove.setParent(None)

        # 按选择顺序显示缩略图
        for index, file in enumerate(files):
            pixmap = QPixmap(file).scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation)
            label = QLabel()
            label.setPixmap(pixmap)
            label.setAlignment(Qt.AlignCenter)
            self.grid_layout.addWidget(label, index // 4, index % 4)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageSelector()
    window.show()
    sys.exit(app.exec_())

代码解释:

  1. ThumbnailDialog:自定义对话框,用于显示指定文件夹中的所有图片缩略图。

    • load_images() 方法会加载并显示文件夹中的图片。
    • select_image() 方法记录用户点击的图片顺序。
    • exec_() 方法返回用户按点击顺序选择的图片列表。
  2. ImageSelector:主窗口类。

    • open_folder_dialog() 打开文件夹选择对话框,并显示 ThumbnailDialog 对话框,供用户选择图片。
    • display_images() 按选择顺序显示图片缩略图。

运行效果:

用户选择一个文件夹后,自定义的 ThumbnailDialog 会显示文件夹中的所有图片缩略图。用户可以点击选择图片,点击顺序将被记录,并在主窗口中按顺序显示这些图片的缩略图。

相关推荐
Coder_Boy_15 小时前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范实现细节
大数据·开发语言·人工智能·spring boot
lly20240615 小时前
SQL SELECT 语句详解
开发语言
superman超哥15 小时前
Rust 异步时间管理核心:Tokio 定时器实现机制深度剖析
开发语言·rust·编程语言·rust异步时间管理核心·tokio定时器实现机制·tokio定时器
朔北之忘 Clancy15 小时前
2025 年 9 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·数学·青少年编程·题解
玛丽莲茼蒿15 小时前
javaSE 集合框架(五)——java 8新品Stream类
java·开发语言
wjs202416 小时前
SQLite Glob 子句详解
开发语言
youyicc16 小时前
Qt连接Pg数据库
开发语言·数据库·qt
开开心心就好16 小时前
图片格式转换工具,右键菜单一键转换简化
linux·运维·服务器·python·django·pdf·1024程序员节
量子炒饭大师16 小时前
【C++入门】Cyber底码作用域的隔离协议——【C++命名空间】(using namespace std的原理)
开发语言·c++·dubbo
骥龙16 小时前
1.2下、工欲善其事:物联网安全研究环境搭建指南
python·物联网·安全