问题描述
我正在使用QtWebEngineWidgets
,QtWebChannel
创建PyQt5应用程序,该应用程序使用HTML,CSS,JavaScript.
I'm using QtWebEngineWidgets
, QtWebChannel
to create PyQt5 application, which uses HTML, CSS, JavaScript.
以常规方式(即python main.py
按如下所示导入HTML
Importing HTML as below,
current_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(current_dir, "index.html")
url = QtCore.QUrl.fromLocalFile(filename)
导入CSS,JavaScript文件如下
Importing CSS, JavaScript files as below,
# 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>
现在,我正在尝试使用pyinstaller
创建一个独立的.exe
文件.
Now, I'm trying to create a standalone .exe
file using pyinstaller
.
我从此处尝试过,但没有成功.
I have tried from here with no success.
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 command:
pyinstaller --onefile --windowed main.py
我需要在生成的.exe
文件中手动添加静态文件,以按预期方式工作.我想将其包含在.exe
文件本身中.如何获得?
I need to manually add static files at generated .exe
file to work as expected. Which I want to include it in .exe
file itself. How to get this?
推荐答案
根据您的问题,您可以假定项目的结构如下:
From your question you can presume that the structure of your project is as follows:
├── index.html
├── jquery.js
├── main.py
├── my_custom.js
└── styles.css
针对您的情况,有2种选择:
For your case there are 2 options:
-
使用
--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 __name__ == "__main__":
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"选项:
If you want to add external resources to the executable then you must use the "--add-data" option:
pyinstaller --onefile --windowed --add-data="index.html:." --add-data="jquery.js:." --add-data="my_custom.js:." --add-data="styles.css:." main.py
对于Windows,将:"更改为;".
For windows change ":" with ";".
使用 .qrc
使用此方法,您将使用pyrcc5将文件(.html,.css,.js等)转换为.py代码,为此,您必须执行以下步骤:
With this method you will convert the files (.html, .css, .js, etc) into .py code using pyrcc5 for this you must follow the following steps:
2.1.在项目文件夹中创建一个名为resource.qrc的文件,其中包含以下内容:
2.1. Create a file called resource.qrc with the following content in the project folder:
<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:
2.2 Convert it to .py using pyrcc5:
pyrcc5 resource.qrc -o resource_rc.py
2.3导入resource_rc.py文件,并在main.py文件中使用带有模式"qrc"的url:
2.3 Import the resource_rc.py file and use the url with schema "qrc" in the main.py file:
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使用初始命令编译项目
2.4 Compile the project using your initial command
pyinstaller --onefile --windowed main.py
这篇关于如何在pyinstaller中添加静态(html,css,js)文件以创建独立的exe文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!