《PyInstaller打包实战指南》第十四节 单文件模式打包PyQt5

第十四节 单文件模式打包PyQt5

打包示例源码下载:

请看文章末尾
 

版本信息:

PyQt5==5.11.3

pyinstaller==3.5

打包系统:

Windows10

打包PyQt5程序其实没有什么难度,笔者这里主要想介绍下PyQt5的qrc文件,因为它给我们提供了另一种打包资源文件的方式。另外还会讲下在PyQt5版本较新时打包所出现的一个问题。

现打包以下示例代码:

import os
import sys
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel


def res_path(relative_path):
    """获取资源绝对路径"""
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


class Demo(QLabel):
    def __init__(self):
        super(Demo, self).__init__()
        # 设置窗口图标
        self.setWindowIcon(QIcon(res_path('res/icon.ico')))

        # 创建QPixmap图片对象
        pic = QPixmap(res_path('res/pic.jpg'))

        # 设置QLabel控件的图片
        self.setPix

猜你喜欢

转载自blog.csdn.net/La_vie_est_belle/article/details/95763055