Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 缺少使用PyInstaller的按钮图标_Python 3.x_Icons_Pyinstaller_Executable - Fatal编程技术网

Python 3.x 缺少使用PyInstaller的按钮图标

Python 3.x 缺少使用PyInstaller的按钮图标,python-3.x,icons,pyinstaller,executable,Python 3.x,Icons,Pyinstaller,Executable,当我尝试使用pyinstaller创建一个onefile可执行文件时,所有按钮图标都会被删除。 我想使用相对路径,以便.exe也在其他计算机上运行。 我发现了一些旧帖子,但我没有让它们发挥作用 Python代码: import sys from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(QtWidgets.QMainWindow): def __init__(self, debugging=False):

当我尝试使用pyinstaller创建一个onefile可执行文件时,所有按钮图标都会被删除。 我想使用相对路径,以便.exe也在其他计算机上运行。 我发现了一些旧帖子,但我没有让它们发挥作用

Python代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(QtWidgets.QMainWindow):

    def __init__(self, debugging=False):
        super().__init__()
        self.__setupUi()
        self.__retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)
        self.show()

    def __setupUi(self):        
        self.setObjectName('MainWindow')
        self.resize(400, 100)
        
        self.centralwidget=QtWidgets.QWidget(self)
        self.centralwidget.setObjectName('centralwidget')
        self.verticalLayout=QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName('verticalLayout')
        self.pushButton=QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setText('')
        icon=QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap('Icons/test.png'), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton.setIcon(icon)
        self.pushButton.setIconSize(QtCore.QSize(32, 32))
        self.pushButton.setObjectName('pushButton')
        self.verticalLayout.addWidget(self.pushButton)
        self.setCentralWidget(self.centralwidget)

    def __retranslateUi(self):
        _translate=QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate('MainWindow', 'MainWindow'))


if __name__=='__main__':
    app=QtWidgets.QApplication(sys.argv)
    uiObj=Ui_MainWindow()
    sys.exit(app.exec())
PyInstaller命令:

python -m PyInstaller --onefile --windowed --add-data Icons/*.png;Icons test.py
以及.spec内容:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['test.py'],
             pathex=['C:\\Users\\User\\Desktop\\Neuer Ordner'],
             binaries=[],
             datas=[('Icons/*.png', 'Icons')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False )
那么如何解决这个问题呢?
Thx.

所以今天我找到了解决方案。这篇旧文章真的很有帮助()。 例如,我的文件夹结构如下:

Folder
    - test.py
    - absolutePath.py
    - Icons
生成PNG的绝对路径需要脚本absolutePath.py。文件夹图标包含PNG文件

绝对路径.py:

import sys
import os

def absolutePath(relative_path):
    base_path=getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) 
    return os.path.join(base_path, relative_path)
test.py:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from absolutePath import absolutePath


class Ui_MainWindow(QtWidgets.QMainWindow):

    def __init__(self, debugging=False):
        super().__init__()
        self.__setupUi()
        self.__retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)
        self.show()

    def __setupUi(self):        
        self.setObjectName('MainWindow')
        self.resize(400, 100)
        
        self.centralwidget=QtWidgets.QWidget(self)
        self.centralwidget.setObjectName('centralwidget')
        self.verticalLayout=QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName('verticalLayout')
        self.pushButton=QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setText('')
        icon=QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(absolutePath('Icons/icon.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton.setIcon(icon)
        self.pushButton.setIconSize(QtCore.QSize(32, 32))
        self.pushButton.setObjectName('pushButton')
        self.verticalLayout.addWidget(self.pushButton)
        self.setCentralWidget(self.centralwidget)

    def __retranslateUi(self):
        _translate=QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate('MainWindow', 'MainWindow'))


if __name__=='__main__':
    app=QtWidgets.QApplication(sys.argv)
    uiObj=Ui_MainWindow()
    sys.exit(app.exec())
最后是PyInstaller命令:

python -m PyInstaller --onefile --windowed --add-data Icons\\*.png;Icons\\ test.py
这样生成的可执行文件包括文件夹图标中的所有PNG 叫做图标。这些图片可以在运行时找到并显示。可执行文件也可以在其他计算机上使用