欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+PyQt6編寫一個圖片播放器

 更新時間:2025年02月18日 09:09:17   作者:一晌小貪歡  
這篇文章主要為大家詳細介紹了Python如何結(jié)合PyQt6編寫一個圖片播放器,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的小伙伴可以參考下

1、背景介紹

我們可以利用pyqt6創(chuàng)建一個圖片查看器,其中包括,選則一個包含多張圖片的文件夾,然后點擊按鈕【下一頁】或者【上一頁】進行圖片的翻頁

2、庫的安裝

用途安裝
PyQt6界面設(shè)計pip install PyQt6 -i https://pypi.tuna.tsinghua.edu.cn/simple/

3、核心代碼

①:圖片展示

def showImage(self, imagePath):
    self.current_pixmap = QPixmap(imagePath)
    self.resizeImage()

②:自適應尺寸縮放

def resizeImage(self):
    if self.current_pixmap:
        # 獲取標簽的大小
        label_size = self.lb.size()
        # 保持縱橫比縮放圖片以適應標簽大小
        scaled_pixmap = self.current_pixmap.scaled(
            label_size.width(),
            label_size.height(),
            Qt.AspectRatioMode.KeepAspectRatio,
            Qt.TransformationMode.SmoothTransformation
        )
        self.lb.setPixmap(scaled_pixmap)

4、完整代碼

import sys
import os

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QApplication, QLabel, QFileDialog, QPushButton, QHBoxLayout


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        self.setWindowTitle("圖片瀏覽器")
        self.resize(500, 350)

        # 修改 QLabel 的設(shè)置
        self.lb = QLabel()
        self.lb.setMinimumSize(200, 200)  # 設(shè)置最小尺寸
        self.lb.setAlignment(Qt.AlignmentFlag.AlignCenter)  # 居中對齊

        # 選擇文件夾按鈕
        self.selectFolderButton = QPushButton("選擇文件夾")
        self.selectFolderButton.clicked.connect(self.selectFolder)

        # 上一張按鈕
        self.prevButton = QPushButton("上一張")
        self.prevButton.clicked.connect(self.showPrevImage)

        # 下一張按鈕
        self.nextButton = QPushButton("下一張")
        self.nextButton.clicked.connect(self.showNextImage)

        # 默認圖片列表
        self.imageFiles = []
        self.currentIndex = -1
        self.current_pixmap = None  # 添加存儲當前圖片的變量

        # 布局設(shè)置
        layout = QVBoxLayout()

        # 圖片標簽占據(jù)主要空間
        layout.addWidget(self.lb, 1)  # 添加拉伸因子1

        # 按鈕布局
        buttonLayout = QHBoxLayout()
        buttonLayout.addWidget(self.prevButton)
        buttonLayout.addWidget(self.selectFolderButton)
        buttonLayout.addWidget(self.nextButton)

        layout.addLayout(buttonLayout)
        self.setLayout(layout)

    def selectFolder(self):
        folderPath = QFileDialog.getExistingDirectory(self, "選擇文件夾")
        if folderPath:
            self.imageFiles = [os.path.join(folderPath, f) for f in os.listdir(folderPath)
                               if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
            self.currentIndex = 0
            if self.imageFiles:
                self.showImage(self.imageFiles[self.currentIndex])

    def showImage(self, imagePath):
        self.current_pixmap = QPixmap(imagePath)
        self.resizeImage()

    def showPrevImage(self):
        if self.imageFiles and self.currentIndex > 0:
            self.currentIndex -= 1
            self.showImage(self.imageFiles[self.currentIndex])

    def showNextImage(self):
        if self.imageFiles and self.currentIndex < len(self.imageFiles) - 1:
            self.currentIndex += 1
            self.showImage(self.imageFiles[self.currentIndex])

    def resizeEvent(self, event):
        super().resizeEvent(event)
        self.resizeImage()

    def resizeImage(self):
        if self.current_pixmap:
            # 獲取標簽的大小
            label_size = self.lb.size()
            # 保持縱橫比縮放圖片以適應標簽大小
            scaled_pixmap = self.current_pixmap.scaled(
                label_size.width(),
                label_size.height(),
                Qt.AspectRatioMode.KeepAspectRatio,
                Qt.TransformationMode.SmoothTransformation
            )
            self.lb.setPixmap(scaled_pixmap)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec())

最后效果

以上就是Python+PyQt6編寫一個圖片播放器的詳細內(nèi)容,更多關(guān)于Python圖片播放器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論