开源python双屏图片浏览器软件


源代码

需要安装pyqt5这个库

python 复制代码
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QPushButton, QFileDialog, QAction, QSlider, QHBoxLayout, QWidget
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QSize
import sys
import os


class ImageViewer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("图片浏览器")
        self.setGeometry(100, 100, 800, 400)

        self.image_label_1 = QLabel(self)
        self.image_label_1.setAlignment(Qt.AlignCenter)
        self.image_name_label_1 = QLabel(self)
        self.image_name_label_1.setAlignment(Qt.AlignCenter)

        self.image_label_2 = QLabel(self)
        self.image_label_2.setAlignment(Qt.AlignCenter)
        self.image_name_label_2 = QLabel(self)
        self.image_name_label_2.setAlignment(Qt.AlignCenter)

        self.slider = QSlider(Qt.Horizontal, self)
        self.slider.valueChanged.connect(self.slider_value_changed)

        self.current_index = 0
        self.image_paths = []

        layout = QHBoxLayout()
        layout_1 = QVBoxLayout()
        layout_1.addWidget(self.image_label_1)
        layout_1.addWidget(self.image_name_label_1)
        layout_2 = QVBoxLayout()
        layout_2.addWidget(self.image_label_2)
        layout_2.addWidget(self.image_name_label_2)
        layout.addLayout(layout_1)
        layout.addLayout(layout_2)

        vbox = QVBoxLayout()
        vbox.addLayout(layout)
        vbox.addWidget(self.slider)

        central_widget = QWidget(self)
        central_widget.setLayout(vbox)
        self.setCentralWidget(central_widget)

        self.create_menu()
        self.load_images()

    def create_menu(self):
        open_folder_action = QAction("打开文件夹", self)
        open_folder_action.triggered.connect(self.open_folder)

        menubar = self.menuBar()
        file_menu = menubar.addMenu("文件")
        file_menu.addAction(open_folder_action)

    def open_folder(self):
        folder_dialog = QFileDialog.getExistingDirectory(self, "选择文件夹")
        if folder_dialog:
            self.image_paths = self.get_image_files(folder_dialog)
            if self.image_paths:
                self.current_index = 0
                self.load_images()

    def get_image_files(self, folder_path):
        image_files = []
        for file_name in os.listdir(folder_path):
            if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp')):
                image_files.append(os.path.join(folder_path, file_name))
        return image_files

    def load_images(self):
        if self.image_paths:
            if self.current_index < len(self.image_paths):
                image_path_1 = self.image_paths[self.current_index]
                pixmap_1 = QPixmap(image_path_1)
                self.image_label_1.setPixmap(
                    pixmap_1.scaled(QSize(300, 300), aspectRatioMode=Qt.AspectRatioMode.KeepAspectRatio))
                self.image_name_label_1.setText(os.path.basename(image_path_1))
            else:
                self.image_label_1.clear()
                self.image_name_label_1.clear()

            if self.current_index + 1 < len(self.image_paths):
                image_path_2 = self.image_paths[self.current_index + 1]
                pixmap_2 = QPixmap(image_path_2)
                self.image_label_2.setPixmap(
                    pixmap_2.scaled(QSize(300, 300), aspectRatioMode=Qt.AspectRatioMode.KeepAspectRatio))
                self.image_name_label_2.setText(os.path.basename(image_path_2))
            else:
                self.image_label_2.clear()
                self.image_name_label_2.clear()

            self.slider.setMinimum(0)
            self.slider.setMaximum(len(self.image_paths) - 1)
            self.slider.setValue(self.current_index)

    def slider_value_changed(self, value):
        self.current_index = value
        self.load_images()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    viewer = ImageViewer()
    viewer.show()
    sys.exit(app.exec_())
相关推荐
喵叔哟2 分钟前
重构代码之移动字段
java·数据库·重构
念白4436 分钟前
智能病历xml提取
数据库·sql·oracle
qingy_204610 分钟前
【JavaWeb】JavaWeb入门之XML详解
数据库·oracle
大数据面试宝典14 分钟前
用AI来写SQL:让ChatGPT成为你的数据库助手
数据库·人工智能·chatgpt
努力的小雨19 分钟前
快速上手 KSQL:轻松与数据库交互的利器
数据库·经验分享
封步宇AIGC19 分钟前
量化交易系统开发-实时行情自动化交易-3.4.1.2.A股交易数据
人工智能·python·机器学习·数据挖掘
何曾参静谧19 分钟前
「Py」Python基础篇 之 Python都可以做哪些自动化?
开发语言·python·自动化
Gentle58621 分钟前
labview中连接sql server数据库查询语句
数据库·labview
Gentle58622 分钟前
labview用sql server数据库存取数据到一个单元格
数据库·labview