PyInstaller 不会将 PyQt 的图像加载到 GUI [英] PyInstaller won't load the PyQt's images to the GUI

查看:38
本文介绍了PyInstaller 不会将 PyQt 的图像加载到 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将脚本传递给可执行文件时遇到一些复杂情况,但我终于成功了.主要问题是 PyInstaller 不会将图像加载到 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.

它应该是这样的:

这是它的样子:

而且我似乎找不到问题所在,这是 .spec 文件:

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:\Users\Poblet\ManGet\HeyMang\pyinstaller\Hey 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:\users\poblet\appdata\local\temp\tmpr34zmp
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.

整个源代码(减去 PyInstaller 文件)在这里.

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

感谢您的帮助.

推荐答案

我能够解决这个问题,这也应该对其他人有所帮助:

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

  • 使用以下命令创建 .spec 文件:

  • Create the .spec file with the following command:

python Makespec.py --noconsole --icon="youricon.ico" --name="App name" program.py

  • 打开 .spec 文件(例如:App name/App name.spec),您应该会看到如下内容:

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

    a.binaries之前,你应该添加这段代码:

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

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

    因此,当 PyInstaller 读取 .spec 文件时,编译器会将图像传递到 dist 目录.

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

    现在我们需要创建 .qrc 文件,它将加载我们的图像.这个文件应该是这样的:

    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>
    

    很明显,你的图片.而这个需要编译成.py格式,命令如下:

    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.

    这篇关于PyInstaller 不会将 PyQt 的图像加载到 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆