Pyt*_*der 5 python pyqt pyinstaller pyqt5 qwebengineview
我正在使用 QtWebEngineWidgets,QtWebChannel创建 PyQt5 应用程序,它使用 HTML、CSS、JavaScript。
它工作正常,当我们以一般方式运行时,即, 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)
Run Code Online (Sandbox Code Playgroud)
导入 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>
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试.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)
Run Code Online (Sandbox Code Playgroud)
pyinstaller 命令:
pyinstaller --onefile --windowed main.py
Run Code Online (Sandbox Code Playgroud)
我需要在生成的.exe文件中手动添加静态文件才能按预期工作。我想将它包含在.exe文件本身中。如何得到这个?
根据您的问题,您可以假设您的项目结构如下:
\n\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.html\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 jquery.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my_custom.js\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 styles.css\nRun Code Online (Sandbox Code Playgroud)\n\n对于您的情况,有 2 个选项:
\n\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.html\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 jquery.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my_custom.js\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 styles.css\nRun Code Online (Sandbox Code Playgroud)\n\n如果要将外部资源添加到可执行文件,则必须使用“--add-data”选项:
\n\npyinstaller --onefile --windowed --add-data="index.html:." --add-data="jquery.js:." --add-data="my_custom.js:." --add-data="styles.css:." main.py\nRun Code Online (Sandbox Code Playgroud)\n\n对于 Windows,将“:”更改为“;”。
使用.qrc
使用此方法,您将使用 Pyrcc5 将文件(.html、.css、.js 等)转换为 .py 代码,为此您必须遵循以下步骤:
\n\n2.1. 在项目文件夹中创建一个名为 resources.qrc 的文件,其中包含以下内容:
\n\n<RCC>\n <qresource prefix="/">\n <file>index.html</file>\n <file>jquery.js</file>\n <file>my_custom.js</file>\n <file>styles.css</file>\n </qresource>\n</RCC>\nRun Code Online (Sandbox Code Playgroud)\n\n2.2 使用pyrcc5将其转换为.py:
\n\npyrcc5 resource.qrc -o resource_rc.py\nRun Code Online (Sandbox Code Playgroud)\n\n2.3 导入resource_rc.py文件并在main.py文件中使用schema为“qrc”的url:
\n\nimport os\nimport sys\n\nfrom PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets\n\n\ndef resource_path(relative_path):\n """ Get absolute path to resource, works for dev and for PyInstaller """\n try:\n # PyInstaller creates a temp folder and stores path in _MEIPASS\n base_path = sys._MEIPASS\n except Exception:\n base_path = os.path.abspath(".")\n\n return os.path.join(base_path, relative_path)\n\n\nif __name__ == "__main__":\n import sys\n\n app = QtWidgets.QApplication(sys.argv)\n view = QtWebEngineWidgets.QWebEngineView()\n\n filename = resource_path("index.html")\n url = QtCore.QUrl.fromLocalFile(filename)\n\n view.load(url)\n view.show()\n sys.exit(app.exec_())\nRun Code Online (Sandbox Code Playgroud)\n\n2.4 使用初始命令编译项目
\n\npyinstaller --onefile --windowed main.py\nRun Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2201 次 |
| 最近记录: |