<p>您的代码存在以下问题:</p>
<ul>
<li><p>您正在COLLECT之后将<code>Tree()</code>添加到a.datas,因此它不会在编译中使用,您必须在COLLECT之前添加它</p></li>
<li><p>您不能再使用{uu file}获取目录路径,而必须使用<a href="https://pythonhosted.org/PyInstaller/runtime-information.html#using-file-and-sys-meipass" rel="nofollow noreferrer">sys._MEIPASS</a></p></li>
</ul>
<p>我亦会作出以下改善:</p>
<ul>
<li>为了使.spec可移植,我将使用<a href="https://pythonhosted.org/PyInstaller/spec-files.html#globals-available-to-the-spec-file" rel="nofollow noreferrer">SPECPATH</a>变量</李>
<li>我添加了第二个参数“wizardUI”来创建带有.ui的字典,我还排除了wizard.py</李>
</ul>
<p>考虑到上述情况,解决方案如下:</p>
<p><strong>main.py</strong></p>
<pre class="lang-py prettyprint-override"><code>from PySide2.QtWidgets import QApplication
from wizardUI.wizard import tutorWizard
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
window = tutorWizard()
window.show()
sys.exit(app.exec_())
</code></pre>
<p><strong>wizard.py</strong></p>
<pre><code>import os
import sys
from PySide2 import QtCore, QtWidgets, QtUiTools
# https://stackoverflow.com/a/42615559/6622587
if getattr(sys, 'frozen', False):
# If the application is run as a bundle, the pyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
current_dir = os.path.join(sys._MEIPASS, "wizardUI")
else:
current_dir = os.path.dirname(os.path.abspath(__file__))
class tutorWizard(QtWidgets.QWizard):
""" Contains introduction tutorial """
def __init__(self, parent=None):
super(tutorWizard, self).__init__(parent)
self.setWindowTitle("Introduction tutorial")
pages = self.findPages()
self.initPages(pages)
def findPages(self):
ui_files = []
cnt = 1
while len(ui_files) < 15:
for file in os.listdir(current_dir):
if file.startswith("{}.".format(cnt)):
ui_files.append(os.path.join(current_dir, file))
cnt += 1
return ui_files
def initPages(self, files):
loader = QtUiTools.QUiLoader()
for i in files:
file = QtCore.QFile(str(i))
if file.open(QtCore.QFile.ReadOnly):
page = loader.load(file)
self.addPage(page)
</code></pre>
<p><strong>main.spec</strong></p>
<pre><code># -*- mode: python ; coding: utf-8 -*-
# https://stackoverflow.com/a/50402636/6622587
import os
spec_root = os.path.abspath(SPECPATH)
block_cipher = None
a = Analysis(['main.py'],
pathex=[spec_root],
binaries=[],
datas=[],
hiddenimports=['PySide2.QtXml', 'packaging.specifiers', 'packaging.requirements'],
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='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
a.datas += Tree(os.path.join(spec_root, 'wizardUI'), 'wizardUI', excludes=["*.py"])
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='main')
</code></pre>
<hr/>
<p>另一种选择是使用<a href="https://doc.qt.io/qt-5/resources.html" rel="nofollow noreferrer">^{<cd2>}</a>而不是数据</p>
<p><strong>resource.qrc</strong></p>
<pre class="lang-xml prettyprint-override"><code><RCC>
<qresource prefix="/">
<file>wizardUI/1.welcomePage.ui</file>
<file>wizardUI/2.graphicsScene.ui</file>
<file>wizardUI/3.graphicsSceneText.ui</file>
<file>wizardUI/4.textDialog.ui</file>
<file>wizardUI/5.codeDialog.ui</file>
<file>wizardUI/6.graphicsSceneBox.ui</file>
<file>wizardUI/7.graphicsScenePixmap.ui</file>
<file>wizardUI/8.graphicsSceneShrCt.ui</file>
<file>wizardUI/9.toolbox.ui</file>
<file>wizardUI/10.toolBoxBtns.ui</file>
<file>wizardUI/11.toolBoxShrCt.ui</file>
<file>wizardUI/12.propertyBox.ui</file>
<file>wizardUI/13.printing.ui</file>
<file>wizardUI/14.settings.ui</file>
<file>wizardUI/15.coclusion.ui</file>
</qresource>
</RCC>
</code></pre>
<p>然后使用pyside2 rcc将其转换为.py:</p>
<pre><code>pyside2-rcc resource.qrc -o resource_rc.py
</code></pre>
<p>然后您必须修改脚本:</p>
<p><strong>main.py</strong></p>
<pre class="lang-py prettyprint-override"><code>from PySide2.QtWidgets import QApplication
from wizardUI.wizard import tutorWizard
import sys
import resource_rc
if __name__ == "__main__":
app = QApplication(sys.argv)
window = tutorWizard()
window.show()
sys.exit(app.exec_())
</code></pre>
<p><strong>wizard.py</strong></p>
<pre class="lang-py prettyprint-override"><code>from PySide2 import QtCore, QtWidgets, QtUiTools
class tutorWizard(QtWidgets.QWizard):
""" Contains introduction tutorial """
def __init__(self, parent=None):
super(tutorWizard, self).__init__(parent)
self.setWindowTitle("Introduction tutorial")
pages = self.findPages()
self.initPages(pages)
def findPages(self):
ui_files = []
cnt = 1
while len(ui_files) < 15:
it = QtCore.QDirIterator(":/wizardUI")
while it.hasNext():
filename = it.next()
name = QtCore.QFileInfo(filename).fileName()
if name.startswith("{}.".format(cnt)):
ui_files.append(filename)
cnt += 1
return ui_files
def initPages(self, files):
loader = QtUiTools.QUiLoader()
for i in files:
file = QtCore.QFile(str(i))
if file.open(QtCore.QFile.ReadOnly):
page = loader.load(file)
self.addPage(page)
</code></pre>
<p>最后,您的项目结构如下所示:</p>
<pre><code>├── main.py
├── main.spec
├── resource.qrc
├── resource_rc.py
└── wizardUI
├── 10.toolBoxBtns.ui
├── 11.toolBoxShrCt.ui
├── 12.propertyBox.ui
├── 13.printing.ui
├── 14.settings.ui
├── 15.coclusion.ui
├── 1.welcomePage.ui
├── 2.graphicsScene.ui
├── 3.graphicsSceneText.ui
├── 4.textDialog.ui
├── 5.codeDialog.ui
├── 6.graphicsSceneBox.ui
├── 7.graphicsScenePixmap.ui
├── 8.graphicsSceneShrCt.ui
├── 9.toolbox.ui
└── wizard.py
</code></pre>
<hr/>
<p><sup>找到两种解决方案<a href="https://github.com/eyllanesc/stackoverflow/tree/master/questions/57185927" rel="nofollow noreferrer">here</a></sup></p>