我有一个简单的应用程序,试图将其捆绑为可执行文件。我在将.qss样式表的路径添加到.spec文件时遇到问题,但是当我尝试从bin目录运行exe时会导致错误。我收到错误消息:
Exception:[Errno 2] No such file or directory: 'FrontEnd\\Styles\\App.qss'
如果我将可执行文件放在项目的基本目录中,则此文件将运行,但是我在使用--onefile标志的情况下,没有其他地方。我还有其他文件放入.spec文件中,它们已被正确拾取。但这似乎是.qss的问题。
Jernel.spec
# -*- mode: python -*-
block_cipher = None
app_resources = [("FrontEnd/Styles/App.qss", "FrontEnd/Styles/"),
("Assets/Icons/*.png", "Assets/Icons/")]
a = Analysis(['Jernel.py'],
pathex=
['C:\\Users\\Fidel\\Dropbox\\Create\\CProjects\\MyProjects\\Jernel'],
binaries=[],
datas=app_resources,
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,
a.binaries,
a.zipfiles,
a.datas,
[],
name='Jernel',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True,
icon='C:\\Users\\Fidel\\Dropbox\\Create\\CProjects\\MyProjects\\Jernel\\Assets\\Icons\\Windows\\AppIcon.ico')
Jernel.py
import sys
import ctypes
import pathlib
from FrontEnd import QMainWindow, QApplication, Qt, QDockWidget,
QGridLayout, QDockWidget, QIcon, QSize, QLabel
from BackEnd import Navigator
class Jernel(QMainWindow):
def __init__(self, parent = None):
super().__init__(parent)
self.stack = Navigator()
self.setWindowFeatures()
self.setWindowContent()
self.setAppIcons()
self.setMenuBar()
self.show()
def setWindowFeatures(self):
self.setGeometry(1280, 1280, 1280, 1280)
self.setWindowFlags(Qt.FramelessWindowHint)
def setAppIcons(self):
print("Adding Icons")
appIcon = QIcon()
sizes = [512, 256, 48, 32, 24, 16]
for size in sizes:
appIcon.addFile(str(pathlib.Path("Assets/Icons/AppIcon.png")),
QSize(size, size))
self.setWindowIcon(appIcon)
print("Finished")
def setMenuBar(self):
self.menuDock = QDockWidget()
def setWindowContent(self):
self.setCentralWidget(self.stack)
if __name__ == '__main__':
try:
jernelApp = QApplication(sys.argv)
jernelApp.setApplicationName("Jernel")
jernelApp.setApplicationDisplayName("Jernel")
jernelApp.setApplicationVersion("1.0.0")
if sys.platform == "win32":
jernelAppID = "FiddyTT.GuiApp.1.0.0"
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(jernelAppID)
window = Jernel()
with open(str(pathlib.Path("FrontEnd/Styles/App.qss")), "r") as stylesSheet:
window.setStyleSheet(stylesSheet.read())
jernelApp.exec_()
except Exception as exe:
print("Exception:{}".format(exe))
finally:
check = input()
仅加载sytleSheet时,图标不会引起异常。预先感谢您的帮助。