PyInstaller won't load the PyQt's images to the GUI

后端 未结 4 1450
忘了有多久
忘了有多久 2020-12-04 23:05

I\'ve been having some complications to pass my script into an executable, but I finally managed to. The main problem is that PyInstaller doesn\'t load the images to the GUI

相关标签:
4条回答
  • 2020-12-04 23:15

    I was able to solve this, and this should help others as well:

    • Create the .spec file with the following command:

      python Makespec.py --noconsole --icon="youricon.ico" --name="App name" program.py
      
    • Open the .spec file (eg.: App name/App name.spec) and you should see something like this:

      a = Analysis([
              os.path.join(HOMEPATH,'support\\_mountzlib.py'),
              os.path.join(HOMEPATH,'support\\useUnicode.py'),
              'program.py'
          ], pathex=[
              'C:\\Your\\User\\Path\\To\\pyinstaller'
      ])
      pyz = PYZ(a.pure)
      exe = EXE(
              pyz,
              a.scripts,
              exclude_binaries=1,
              name=os.path.join('build\\pyi.win32\\App Name', 'App Name.exe'),
              debug=False,
              strip=False,
              upx=True,
              console=False , icon='youricon.ico'
      )
      coll = COLLECT(
              exe,
              a.binaries,
              a.zipfiles,
              a.datas,
              strip=False,
              upx=True,
              name=os.path.join('dist', 'Hey Mang!')
      )
      app = BUNDLE(coll, name=os.path.join('dist', 'Hey Mang!.app'))
      

      And before a.binaries you should add this piece of code:

             Tree('C:\\Your\\App\\Path\\To\\Images'),
      

      So when PyInstaller reads the .spec file, the compiler will pass the image to the dist directory.

    • Now we need to create the .qrc file, which will load our images. And this file should look something like this:

      <RCC>
        <qresource prefix="/" >
          <file>img/image1.png</file>
          <file>img/image2.png</file>
          <file>img/image3.png</file>
        </qresource>
      </RCC>
      

      With your images, obviously. And this needs to be compiled to .py format, with the following command:

      pyrcc4 -o images.qrc images_qr.py
      
    • And finally, we need to add this to our script, for example like this:

      import images_qr
      
      ...
      
      self.setWindowIcon(QtGui.QIcon(':/img/image1.png')) # The colon must be there
      

    And once you compile you should see the images just fine, like this:

    I hope this helps everyone with the same issue. Remember to give the proper image paths and to add the colon to your images.

    0 讨论(0)
  • 2020-12-04 23:15

    If you are using Qt Designer, you can add icons and images without much difficulty. Follow the tutorial here http://doc.qt.io/qt-4.8/designer-resources.html

    Tested on PySide 1.2.2 with Qt Designer 4.8.5 and Pyinstaller 2.1

    0 讨论(0)
  • 2020-12-04 23:21

    Move exe file to folder where image folder is located or in the same folder where the main.py is.

    See this

    0 讨论(0)
  • 2020-12-04 23:39

    A quick update to the method below. First off - this is a great way to easily include images in a PyQt/Pyside app. For Pyside simply use pyside-rcc.exe rather than pyrcc4.

    Secondly, I'm not convinced that the Tree(...) command is even necessary. After creating a dist folder with this method, I removed all the image files that were copied in with the Tree.. command. My executable still works and the images are displayed correctly. So it may be that we simply need to create the qrc file and run the pyrcc program, making sure that the python modules are updated with the colon prefix to iamge file paths.

    0 讨论(0)
提交回复
热议问题