当前位置: 代码网 > it编程>前端脚本>Python > Python+PyQt6编写一个图片播放器

Python+PyQt6编写一个图片播放器

2025年02月18日 Python 我要评论
1、背景介绍我们可以利用pyqt6创建一个图片查看器,其中包括,选则一个包含多张图片的文件夹,然后点击按钮【下一页】或者【上一页】进行图片的翻页2、库的安装库用途安装pyqt6界面设计pip inst

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com