Pyinstaller 创建的 PyQt 应用程序中的图标在其他计算机上不起作用

Icons in PyQt app created by Pyinstaller won't work on other computers

好的,所以我用pyqt编写了一个应用程序,然后使用pyinstaller准备了一个exe文件(一个文件)。只要应用程序在我的计算机上,一切都可以正常工作。但是当我尝试在其他设备上 运行 它时,应用程序图形用户界面中的图标不会显示。这让我得出一个结论,即 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)

编辑1: 我正在使用这个功能:

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')]

编辑2: 现在这是我的新规范文件:

# -*- 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')