好的,所以我在pyqt中编写了一个应用程序,然后使用pyinstaller编写了一个exe文件(一个文件)。只要应用程序在我的计算机上,一切正常。但是当我尝试在其他设备上运行它时,应用程序gui中的图标将无法显示。这让我得出结论,pyinstaller不在exe文件中包含这些图标,而是从我计算机上的文件夹中使用它们。我该如何解决这个问题?
在我的python代码中,我包含这样的图标:
self.TraceCheckBox.setIcon(QtGui.QIcon('d:/path/to/icons/icon1.png'))
并且像这样:
icon.addPixmap(QtGui.QPixmap(_fromUtf8("d:/path/to/icons/icon2.png")), QtGui.QIcon.Disabled, QtGui.QIcon.On)
EDIT1: 我正在使用这个功能:
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)
现在我正在访问这样的图标:
self.TraceCheckBox.setIcon(QtGui.QIcon(resource_path('icon1.png')))
这是我的spec文件:
# -*- mode: python -*-
a = Analysis(['name.py'],
pathex=['D:\\my\\path\\app'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='name.exe',
debug=False,
strip=None,
upx=True,
console=False , version='version.txt', icon='road.ico')
现在我应该把这条线放到哪里才能使它工作? :
a.datas += [('images/icon1.png', 'D:\\my\\path\\to\\icons\\icon1.png','DATA')]
EDIT2: 现在这是我的新规范文件:
# -*- mode: python -*-
a = Analysis(['name.py'],
pathex=['D:\\my\\path\\app'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
pyz = PYZ(a.pure)
a.datas += [('images/red_dot1.png', 'D:\\my\\path\\to\\icons\\icons\\red_dot1.png','DATA'),('images/green_dot1.png','D:\\my\\path\\to\\icons\\icons\\green_dot1.png','DATA'),('images/repeat.png','D:\\my\\path\\to\\icons\\icons\\repeat.png','DATA'),('images/calibrate.png','D:\\my\\path\\to\\icons\\icons\\calibrate.png','DATA'),('images/report.png','D:\\my\\path\\to\\icons\\icons\\report.png','DATA'),('images/close_connection.png','D:\\my\\path\\to\\icons\\icons\\close_connection.png','DATA'),('images/auto_open.png','D:\\my\\path\\to\\icons\\icons\\auto_open.png','DATA'),('images/open_connection.png','D:\\my\\path\\to\\icons\\icons\\open_connection.png','DATA'),('images/read.png','D:\\my\\path\\to\\icons\\icons\\read.png','DATA')],
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='name.exe',
debug=False,
strip=None,
upx=True,
console=False , version='version.txt', icon='road.ico')
我收到了这个错误:
Traceback (most recent call last):
File "C:\Python27\Scripts\pyinstaller-script.py", line 9, in <module>
load_entry_point('PyInstaller==2.1', 'console_scripts', 'pyinstaller')()
File "C:\Python27\lib\site-packages\PyInstaller\main.py", line 88, in run
run_build(opts, spec_file, pyi_config)
File "C:\Python27\lib\site-packages\PyInstaller\main.py", line 46, in run_build
PyInstaller.build.main(pyi_config, spec_file, **opts.__dict__)
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 1924, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 1873, in build
execfile(spec)
File "roadtrace8.5.spec", line 20, in <module>
console=False , version='version.txt', icon='road.ico')
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 1170, in __init__
strip_binaries=self.strip, upx_binaries=self.upx,
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 1008, in __init__
self.__postinit__()
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 309, in __postinit__
self.assemble()
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 1035, in assemble
toc = addSuffixToExtensions(self.toc)
File "C:\Python27\lib\site-packages\PyInstaller\build.py", line 179, in addSuffixToExtensions
for inm, fnm, typ in toc:
ValueError: too many values to unpack
答案 0 :(得分:1)
我相信你使用--onefile? 使用onedir,您可以检查pyinstaller是否包含那些png,因此我建议您先使用onedir进行尝试。 但是,您可以通过更改.spec文件来向pyinstaller说明获取这些图标:
dict_tree = Tree('path to the folder with icons', prefix = 'nameofthefolder')
coll = COLLECT(exe,
a.binaries,
dict_tree,
a.zipfiles,
a.datas,
strip=None,
upx=True,
name='manage')
所以请提供.spec文件。 当然你应该尝试相对路径,所以在你的代码中改变它,我相信这里的主要问题。 编辑 尝试:
a = Analysis(['name.py'],
pathex=['D:\\my\\path\\app'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
pyz = PYZ(a.pure)
a.datas += [('images/icon1.png', 'D:\\my\\path\\to\\icons\\icon1.png','DATA')]
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='name.exe',
debug=False,
strip=None,
upx=True,
console=False , version='version.txt', icon='road.ico')