开源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_())
相关推荐
AI原吾几秒前
解锁自动化新境界:KeymouseGo,让键盘和鼠标动起来!
运维·python·自动化·计算机外设·keymousego
卡卡_R-Python几秒前
海洋气象编程工具-Python
开发语言·python
北愚3 分钟前
Scrapy爬虫实战——某瓣250
python·scrapy
.别止步春天.5 分钟前
Python中lambda表达式的使用——完整通透版
数据结构·python·算法
RaidenQ12 分钟前
2024.9.20 Python模式识别新国大EE5907,PCA主成分分析,LDA线性判别分析,GMM聚类分类,SVM支持向量机
python·算法·机器学习·支持向量机·分类·聚类
_平凡之路_19 分钟前
解决ubuntu22.04 gnome-terminal 无法启动的问题
linux·运维·python
豆本-豆豆奶22 分钟前
23个Python在自然语言处理中的应用实例
开发语言·python·自然语言处理·编程语音
NiNg_1_23426 分钟前
机器学习之Python中Scikit-Learn(sklearn)入门
python·机器学习·scikit-learn
大王只是带我巡了个山30 分钟前
优化 OR 条件过多导致的查询超时
数据库·mysql·join·or 优化·or 超时·查询超时
你可以自己看44 分钟前
python中函数式编程与高阶函数,装饰器与生成器,异常处理与日志记录以及项目实战
服务器·开发语言·python