程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件??

开发过程中遇到如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件?的解决方法建议,希望对你解决如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件?有所启发或帮助;

根据您的问题,您可以假定您的项目结构如下:

├── index.HTML
├── jquery.Js
├── main.py
├── my_custom.Js
└── styles.CSS

对于您的情况,有2个选项:

  1. 使用 --add-data

    import os
    

    import sys

    from PyQt5 import QtCore, QtWidgets, QtWebENGIneWidgets

    def resource_path(relative_path): “”” Get absolute path to resource, works for dev and for PyInstaller “”“ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(“.”)

    return os.path.join(base_path, relative_path)
    

    if == “”: import sys

    app = QtWidgets.QApplication(sys.argv)
    vIEw = QtWebENGIneWidgets.QWebENGIneVIEw()
    
    filename = resource_path("index.HTML")
    url = QtCore.QUrl.fromLocalfile(fileName)
    
    vIEw.load(url)
    vIEw.show()
    sys.exit(app.exec_())
    

如果要将外部资源添加到可执行文件,则必须使用“ –add-data”选项:

    pyinstaller --onefile --windowed --add-data="index.HTML:." --add-data="jquery.Js:." --add-data="my_custom.Js:." --add-data="styles.CSS:." main.py

对于windows,将“:”更改为“;”。

  1. 使用 .qrc

使用此方法,您将使用pyrcc5将文件(.HTML,.CSS,.Js等)转换为.py代码,为此,您必须执行以下步骤:

2.1。在项目文件夹中创建一个名为resource.qrc的文件,其中包含以下内容:

    <RCC>
    <qresource prefix="/">
        <file>index.HTML</file>
        <file>jquery.Js</file>
        <file>my_custom.Js</file>
        <file>styles.CSS</file>
    </qresource>
</RCC>

2.2使用pyrcc5将其转换为.py:

    pyrcc5 resource.qrc -o resource_rc.py

2.3导入resource_rc.py文件,并在main.py文件中使用带有模式“ qrc”的url:

    import os
import sys

from PyQt5 import QtCore, QtWidgets, QtWebENGIneWidgets

import resource_rc


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    vIEw = QtWebENGIneWidgets.QWebENGIneVIEw()

    url = QtCore.QUrl("qrc:/index.HTML")

    vIEw.load(url)
    vIEw.show()
    sys.exit(app.exec_())

2.4使用初始命令编译项目

    pyinstaller --onefile --windowed main.py

解决方法

我正在使用 QtWebENGIneWidgetsQtWebChAnnel创建使用HTML,CSS,JavaScript的PyQt5应用程序。

当我们以一般方式运行时,它工作正常,即 python main.py

如下导入HTML,

current_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(current_dir,"index.html")
url = QtCore.QUrl.fromLocalFile(fileName)

如下导入CSS,JavaScript文件,

# in index.html
<link rel="stylesheet" href="styles.css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_custom.js"></script>

现在,我尝试使用创建一个独立.exe文件pyinstaller

def resource_path(relative_path):
    """ Get absolute path to resource,works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path,relative_path)

pyinstaller命令:

pyinstaller --onefile --windowed main.py

我需要在生成的.exe文件中手动添加静态文件才能按预期工作。我想将其包含在.exe文件本身中。如何得到这个?

大佬总结

以上是大佬教程为你收集整理的如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件?全部内容,希望文章能够帮你解决如何在pyinstaller中添加静态(html,css,js等)文件以创建独立的exe文件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。