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图片播放器的资料请关注代码网其它相关文章!
发表评论