Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

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

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.

This is how it should look like:

How it should look like

This is how it looks like:

How it looks like

And I can't seem to find the problem, this is the .spec file:

a = Analysis([os.path.join(HOMEPATH,'support\_mountzlib.py'), os.path.join(HOMEPATH,'support\useUnicode.py'), 'programa.py'],
             pathex=['img', 'C:\Users\Poblet\ManGet\HeyMang\pyinstaller'])
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=1,
          name=os.path.join('build\pyi.win32\Hey Mang!', 'Hey Mang!.exe'),
          debug=False,
          icon='heymang.ico',
          strip=False,
          upx=True,
          console=False )
coll = COLLECT( exe,
               Tree('C:\Users\Poblet\ManGet\HeyMang\pyinstaller\img'),
               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 it supposes to grab those images, like it says here:

Warnings written to C:UsersPobletManGetHeyMangpyinstallerHey Mang!warnHey Mang!.txt
checking PYZ
rebuilding outPYZ1.toc because outPYZ1.pyz is missing
building PYZ outPYZ1.toc
checking PKG
rebuilding outPKG3.toc because outPKG3.pkg is missing
building PKG outPKG3.pkg
checking EXE
rebuilding outEXE2.toc because Hey Mang!.exe missing
building EXE from outEXE2.toc
I: SRCPATH [('heymang.ico', None)]
I: Updating icons from ['heymang.ico'] to c:userspobletappdatalocalempmpr34zmp
I: Writing RT_GROUP_ICON 0 resource with 76 bytes
I: Writing RT_ICON 1 resource with 1128 bytes
I: Writing RT_ICON 2 resource with 4264 bytes
I: Writing RT_ICON 3 resource with 9640 bytes
I: Writing RT_ICON 4 resource with 16936 bytes
I: Writing RT_ICON 5 resource with 67624 bytes

And they are in the folder, but they won't work for a reason or another.

The entire source code (minus the PyInstaller files) is here.

I appreciate your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...