简体   繁体   English

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

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

Ok, so I've written an application in pyqt and then prepared an exe file (one file) using pyinstaller. 好的,所以我已经在pyqt中编写了一个应用程序,然后使用pyinstaller准备了一个exe文件(一个文件)。 Everything works fine as long as the application is on my computer. 只要应用程序在我的计算机上,一切都可以正常工作。 But when I try to run it on other devices the icons in the app gui won't display. 但是,当我尝试在其他设备上运行它时,应用程序GUI中的图标将不会显示。 That leads me to a conclusion that pyinstaller is not including those icons in the exe file and is using them from a folder on my computer. 这使我得出一个结论,即pyinstaller在exe文件中不包含这些图标,而是从我计算机上的文件夹中使用它们。 How do I fix this? 我该如何解决?

In my python code I include icons like this: 在我的python代码中,我包括如下图标:

self.TraceCheckBox.setIcon(QtGui.QIcon('d:/path/to/icons/icon1.png'))

and like this: 像这样:

icon.addPixmap(QtGui.QPixmap(_fromUtf8("d:/path/to/icons/icon2.png")), QtGui.QIcon.Disabled, QtGui.QIcon.On)

EDIT1: I am using this function: 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)

And now I am accessing icons like that: 现在,我正在访问像这样的图标:

self.TraceCheckBox.setIcon(QtGui.QIcon(resource_path('icon1.png')))

This is my spec file: 这是我的规格文件:

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

Now where should I put this line to make it work? 现在,我应该在哪里放置此行以使其起作用? :

a.datas += [('images/icon1.png', 'D:\\my\\path\\to\\icons\\icon1.png','DATA')]

EDIT2: Now this is my new spec file: 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')

And I get this error: 我得到这个错误:

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

I believe you use --onefile? 我相信您使用--onefile吗? With onedir you can check if pyinstaller included those png, so I recommend you to try first with onedir. 使用onedir,您可以检查pyinstaller是否包含这些png,因此建议您首先尝试使用onedir。 However, you can say to pyinstaller to get those icons by changing .spec file with: 但是,您可以告诉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')

So please provide .spec file. 因此,请提供.spec文件。 And of course you should try with relative paths so change that in your code, that is I believe the main problem in here. 当然,您应该尝试使用相对路径,以便在代码中进行更改,这就是我认为这里的主要问题。 EDIT Try: 编辑尝试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM