How to include images into exe generated by PyInstaller in Python

时间:2019-01-09 22:26:41

标签: python pyqt5 pyinstaller

I use PyQt5 to develop a GUI application in python, and that works. Now, I would like to generate exe file using PyInstaller, but I don't know how to include image files into exe file. I mean I wish the exe file doesn't rely on external image files any more. I have tried the method below, but it seems doesn't work for me. Pyinstaller and --onefile: How to include an image in the exe file

following is part of my code:

my code:

class DASH(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.animation = Animation()
        self.setCentralWidget(self.animation)
        exitAct = QAction(QIcon(r'car.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(self.close)

spec file:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['C:\\Jiawei\\Work\\python_simulation_V2\\python_visualization.py'],
         pathex=['C:\\Users\\Lujia'],
         binaries=[],
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

a.datas += [('car.png','C:\\Jiawei\\Work\\python_simulation_V2\\images\\car.png', 'Data')]

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      [],
      name='python_visualization',
      debug=False,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      runtime_tmpdir=None,
      console=True )

0 个答案:

没有答案