开源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_())
相关推荐
yma167 分钟前
windows10下使用沙盒多开uiautoanimation可行性验证
python·uiautoanimation
陌夏微秋9 分钟前
STM32单片机芯片与内部47 STM32 CAN内部架构 介绍
数据库·stm32·单片机·嵌入式硬件·架构·信息与通信
HappyAcmen13 分钟前
青训营-豆包MarsCode技术训练营试题解析四十八
开发语言·python·算法
Pingszi22 分钟前
3.阿里云flink&selectdb-py作业
python·阿里云·flink·数仓
新手小袁_J26 分钟前
No Python at ‘C:\Users\MI\AppData\Local\Programs\Python\Python39\python.exe‘
开发语言·python·error·no python
stormjun26 分钟前
基于 Python Django 的二手电子设备交易平台(附源码,文档)
开发语言·python·django·二手电子设备·电子设备售卖·电子设备交易
程序媛小果42 分钟前
基于Django+python的Python在线自主评测系统设计与实现
android·python·django
minstbe43 分钟前
WEB开发 - Flask 入门:Jinja2 模板语法进阶 Python
后端·python·flask
计算机学无涯1 小时前
Spring事务回滚
数据库·sql·spring
web130933203981 小时前
flume对kafka中数据的导入导出、datax对mysql数据库数据的抽取
数据库·kafka·flume