我正在使用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)
如下导入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
文件。
我尝试了here,但没有成功。
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
文件本身中。如何得到这个?
参考方案
根据您的问题,您可以假定您的项目结构如下:
├── index.html
├── jquery.js
├── main.py
├── my_custom.js
└── styles.css
对于您的情况,有2个选项:
使用 --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”选项:
pyinstaller --onefile --windowed --add-data="index.html:." --add-data="jquery.js:." --add-data="my_custom.js:." --add-data="styles.css:." main.py
对于Windows,将“:”更改为“;”。
使用 .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
在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…
Python 3会流行吗? - python我已经学习了一些Python 2和Python 3,似乎Python 2总体上比Python 3更好。这就是我的问题所在。是否有充分的理由真正切换到python 3? 参考方案 总体上,甚至在大多数细节上,Python3都比Python2更好。关于第三方库, Python 3落后于的唯一区域是。使Python变得如此出色的原因不仅在于它作为一种语言的内在特性…
Python-如何检查Redis服务器是否可用 - python我正在开发用于访问Redis Server的Python服务(类)。我想知道如何检查Redis Server是否正在运行。而且如果某种原因我无法连接到它。这是我的代码的一部分import redis rs = redis.Redis("localhost") print rs 它打印以下内容<redis.client.Redis o…
Python-crontab模块 - python我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…
Python:检查是否存在维基百科文章 - python我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…