Python+PyQt6编写一个图片播放器
作者:一晌小贪欢
这篇文章主要为大家详细介绍了Python如何结合PyQt6编写一个图片播放器,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以参考下
1、背景介绍
我们可以利用pyqt6创建一个图片查看器,其中包括,选则一个包含多张图片的文件夹,然后点击按钮【下一页】或者【上一页】进行图片的翻页
2、库的安装
| 库 | 用途 | 安装 |
|---|---|---|
| PyQt6 | 界面设计 | 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 的设置
self.lb = QLabel()
self.lb.setMinimumSize(200, 200) # 设置最小尺寸
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 # 添加存储当前图片的变量
# 布局设置
layout = QVBoxLayout()
# 图片标签占据主要空间
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编写一个图片播放器的详细内容,更多关于Python图片播放器的资料请关注脚本之家其它相关文章!
