Python+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()
②:自適應(yīng)尺寸縮放
def resizeImage(self):
if self.current_pixmap:
# 獲取標(biāo)簽的大小
label_size = self.lb.size()
# 保持縱橫比縮放圖片以適應(yīng)標(biāo)簽大小
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 # 添加存儲當(dāng)前圖片的變量
# 布局設(shè)置
layout = QVBoxLayout()
# 圖片標(biāo)簽占據(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:
# 獲取標(biāo)簽的大小
label_size = self.lb.size()
# 保持縱橫比縮放圖片以適應(yīng)標(biāo)簽大小
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)文章
Python + Requests + Unittest接口自動化測試實例分析
這篇文章主要介紹了Python + Requests + Unittest接口自動化測試,結(jié)合具體實例形式分析了Python使用Requests與Unittest模塊實現(xiàn)接口自動化測試相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
python 導(dǎo)入數(shù)據(jù)及作圖的實現(xiàn)
今天小編就為大家分享一篇python 導(dǎo)入數(shù)據(jù)及作圖的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python正則表達式高效處理文本數(shù)據(jù)的秘訣輕松掌握
當(dāng)談到文本處理和搜索時,正則表達式是Python中一個強大且不可或缺的工具,正則表達式是一種用于搜索、匹配和處理文本的模式描述語言,可以在大量文本數(shù)據(jù)中快速而靈活地查找、識別和提取所需的信息,2023-11-11
Python實現(xiàn)杰卡德距離以及環(huán)比算法講解
這篇文章主要為大家介紹了Python實現(xiàn)杰卡德距離以及環(huán)比算法的示例講解,有需要的朋友可以借鑒參考下2022-02-02
Python中的"沒有那個文件"錯誤(FileNotFoundError)的解決方法詳解
在Python編程中,遇到“沒有那個文件”錯誤(FileNotFoundError)是常見的問題之一,本文將詳細分析這個錯誤的原因,并提供實用的解決方案和指南,有需要的可以參考下2024-11-11
通過Python掃描代碼關(guān)鍵字并進行預(yù)警的實現(xiàn)方法
這篇文章主要介紹了通過Python掃描代碼關(guān)鍵字并進行預(yù)警的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

