使用Python和PyQt6开发图片播放器(附示例代码)

随着计算机技术的飞速发展,图形用户界面(GUI)的应用程序越来越受到人们的青睐。Python作为一种高效、简洁的编程语言,结合PyQt6库,可以轻松创建功能强大的GUI应用程序。本文将介绍如何使用Python和PyQt6开发一个图片播放器,并提供示例代码,帮助读者快速上手。

1、背景介绍

我们可以利用pyqt6创建一个图片查看器,其中包括,选则一个包含多张图片的文件夹,然后点击按钮【下一页】或者【上一页】进行图片的翻页

2、库的安装

用途安装
PyQt6界面设计pip install PyQt6 -i https://pypi.tuna.tsinghua.edu.cn/simple/

3、核心代码

①:图片展示

defshowImage(self,imagePath):self.current_pixmap=QPixmap(imagePath)self.resizeImage()

②:自适应尺寸缩放

defresizeImage(self):ifself.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、完整代码

importsysimportosfromPyQt6.QtCoreimportQtfromPyQt6.QtGuiimportQPixmapfromPyQt6.QtWidgetsimportQWidget,QVBoxLayout,QApplication,QLabel,QFileDialog,QPushButton,QHBoxLayoutclassMyWidget(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=-1self.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)defselectFolder(self):folderPath=QFileDialog.getExistingDirectory(self,\"选择文件夹\")iffolderPath:self.imageFiles=[os.path.join(folderPath,f)forfinos.listdir(folderPath)iff.lower().endswith((\'.png\',\'.jpg\',\'.jpeg\',\'.bmp\',\'.gif\'))]self.currentIndex=0ifself.imageFiles:self.showImage(self.imageFiles[self.currentIndex])defshowImage(self,imagePath):self.current_pixmap=QPixmap(imagePath)self.resizeImage()defshowPrevImage(self):ifself.imageFilesandself.currentIndex>0:self.currentIndex-=1self.showImage(self.imageFiles[self.currentIndex])defshowNextImage(self):ifself.imageFilesandself.currentIndex<len(self.imageFiles)-1:self.currentIndex+=1self.showImage(self.imageFiles[self.currentIndex])defresizeEvent(self,event):super().resizeEvent(event)self.resizeImage()defresizeImage(self):ifself.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())

最后效果

图片[1]-使用Python和PyQt6开发图片播放器(附示例代码)-趣考网

总结

本文详细介绍了如何使用Python和PyQt6开发一个图片播放器。通过本文的学习,读者可以掌握Python和PyQt6的基本用法,并能够独立开发简单的GUI应用程序。希望本文能够对读者有所帮助,激发读者对Python编程和GUI开发的兴趣。未来,读者可以在此基础上,进一步扩展和优化图片播放器的功能,开发出更加实用和美观的应用程序。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享