Python+PyQt6編寫(xiě)一個(gè)圖片播放器
1、背景介紹
我們可以利用pyqt6創(chuàng)建一個(gè)圖片查看器,其中包括,選則一個(gè)包含多張圖片的文件夾,然后點(diǎn)擊按鈕【下一頁(yè)】或者【上一頁(yè)】進(jìn)行圖片的翻頁(yè)
2、庫(kù)的安裝
庫(kù) | 用途 | 安裝 |
---|---|---|
PyQt6 | 界面設(shè)計(jì) | 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) # 居中對(duì)齊 # 選擇文件夾按鈕 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) # 默認(rèn)圖片列表 self.imageFiles = [] self.currentIndex = -1 self.current_pixmap = None # 添加存儲(chǔ)當(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編寫(xiě)一個(gè)圖片播放器的詳細(xì)內(nèi)容,更多關(guān)于Python圖片播放器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python + Requests + Unittest接口自動(dòng)化測(cè)試實(shí)例分析
這篇文章主要介紹了Python + Requests + Unittest接口自動(dòng)化測(cè)試,結(jié)合具體實(shí)例形式分析了Python使用Requests與Unittest模塊實(shí)現(xiàn)接口自動(dòng)化測(cè)試相關(guān)操作技巧,需要的朋友可以參考下2019-12-12python 導(dǎo)入數(shù)據(jù)及作圖的實(shí)現(xiàn)
今天小編就為大家分享一篇python 導(dǎo)入數(shù)據(jù)及作圖的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python3.0與2.X版本的區(qū)別實(shí)例分析
這篇文章主要介紹了Python3.0與2.X版本的區(qū)別,包含了一些常見(jiàn)的區(qū)別及分析,還有筆者的一些感悟,需要的朋友可以參考下2014-08-08Python正則表達(dá)式高效處理文本數(shù)據(jù)的秘訣輕松掌握
當(dāng)談到文本處理和搜索時(shí),正則表達(dá)式是Python中一個(gè)強(qiáng)大且不可或缺的工具,正則表達(dá)式是一種用于搜索、匹配和處理文本的模式描述語(yǔ)言,可以在大量文本數(shù)據(jù)中快速而靈活地查找、識(shí)別和提取所需的信息,2023-11-11Python實(shí)現(xiàn)杰卡德距離以及環(huán)比算法講解
這篇文章主要為大家介紹了Python實(shí)現(xiàn)杰卡德距離以及環(huán)比算法的示例講解,有需要的朋友可以借鑒參考下2022-02-02Python中的"沒(méi)有那個(gè)文件"錯(cuò)誤(FileNotFoundError)的解決方法詳解
在Python編程中,遇到“沒(méi)有那個(gè)文件”錯(cuò)誤(FileNotFoundError)是常見(jiàn)的問(wèn)題之一,本文將詳細(xì)分析這個(gè)錯(cuò)誤的原因,并提供實(shí)用的解決方案和指南,有需要的可以參考下2024-11-11關(guān)于Python中進(jìn)度條的六個(gè)實(shí)用技巧分享
在項(xiàng)目開(kāi)發(fā)過(guò)程中加載、啟動(dòng)、下載項(xiàng)目難免會(huì)用到進(jìn)度條,下面這篇文章主要給大家介紹了關(guān)于Python中進(jìn)度條的六個(gè)實(shí)用技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04通過(guò)Python掃描代碼關(guān)鍵字并進(jìn)行預(yù)警的實(shí)現(xiàn)方法
這篇文章主要介紹了通過(guò)Python掃描代碼關(guān)鍵字并進(jìn)行預(yù)警的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05