我正在使用pyinstaller编译独立的可执行文件。 python脚本使用来自QtDesigner的外部ui文件。我没有运行没有ui文件的可执行文件。我读过有关定义规范文件的信息,但是没有ui文件,我尝试的所有内容都无法正常工作。我不知道我在做什么错。我可以编译它,但是当我删除ui文件并运行可执行文件时,出现错误:
FileNotFoundError: [Errno 2] No such file or directory: 'D:/visu.ui'
如何将该文件集成到可执行文件中?
这是我的代码。 spec文件是使用pyi-makespec生成的,并手动添加了数据:
visu.py
import sys
import os
from PyQt5 import QtWidgets, uic
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)
Ui_MainWindow, QtBaseClass = uic.loadUiType(resource_path('visu.ui'))
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
visu.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>172</width>
<height>122</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>40</x>
<y>50</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Ok</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
visu.spec
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['D:/visu.py'],
pathex=['C:\\Users\\xxx'],
binaries=[],
datas=[("visu.ui", '.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='visu',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='visu')
答案 0 :(得分:0)
我可以肯定,如果您将visu.ui
文件转换为visu.py
pyuic5 visu.ui -o visu.py
那么您将没有任何问题。
pyinstaller -F -w pyinstaller_standalone_executable.py
pyinstaller_standalone_executable.py
import sys
import os
from PyQt5 import QtWidgets #, uic
from visu import Ui_Dialog
'''
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)
Ui_MainWindow, QtBaseClass = uic.loadUiType(resource_path('visu.ui'))
'''
#class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
class MyWindow(QtWidgets.QDialog, Ui_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(
lambda: QtWidgets.QMessageBox.information(None, "Message", self.pushButton.text())
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())