gpt4 book ai didi

python - PyInstaller 不会将 PyQt 的图像加载到 GUI

转载 作者:太空狗 更新时间:2023-10-29 18:02:33 28 4
gpt4 key购买 nike

将我的脚本传递到可执行文件时遇到了一些麻烦,但我终于成功了。主要问题是 PyInstaller 不会将图像加载到 GUI。

它应该是这样的:

How it should look like

它是这样的:

How it looks like

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

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

它应该抓取这些图像,就像它在这里所说的:

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

它们在文件夹中,但由于某种原因它们无法工作。

整个源代码(减去 PyInstaller 文件)是 here .

感谢您的帮助。

最佳答案

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

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

    python Makespec.py --noconsole --icon="youricon.ico" --name="App name" program.py
  • 打开 .spec 文件(例如:应用程序名称/应用程序名称.spec),您应该会看到如下内容:

    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之前你应该添加这段代码:

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

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

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

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

    显然是用你的图片。这需要编译成.py格式,使用以下命令:

    pyrcc4 -o images.qrc images_qr.py
  • 最后,我们需要将其添加到我们的脚本中,例如:

    import images_qr

    ...

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

一旦你编译你应该能看到图像就好了,像这样:

希望对遇到同样问题的大家有所帮助。请记住提供正确的图像路径并在图像中添加冒号。

关于python - PyInstaller 不会将 PyQt 的图像加载到 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11534293/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com