[英]Icons in PyQt app created by Pyinstaller won't work on other computers
好的,所以我已經在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')))
這是我的規格文件:
# -*- 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
我相信您使用--onefile嗎? 使用onedir,您可以檢查pyinstaller是否包含這些png,因此建議您首先嘗試使用onedir。 但是,您可以告訴pyinstaller通過更改.spec文件來獲取這些圖標:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.