2

SOLVED - Solution posted as answer, thanks all for the help.

After compiling my python application using PyQt5 to an executable file, the icons contained in my GUI are removed / not displayed. Specifically QIcon instances added to my Window(QMainWindow) class using self.setWindowIcon(QtGui.QIcon(fpath)) and a QPixmap(f2path) embedded in a QLabel via label.setPixmap(myPixmap).

I have tried to search on this forum for possible solutions but couldn't find a problem solving thread. I've tried to set the absolute filepath as recommended here Bundling data files with PyInstaller (--onefile) and here Missing button icons in pyinstaller

No idea where to start examining problems, there are no errors when compiling with pyinstaller and it runs fine as a python script.

pyinstaller -w -F MY_GUI.py

Thanks in advance!



Example:

import sys
import os

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

import sys
import resource_path # code taken from links above
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow

class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.title = "MyProg"
        self.top = 400
        self.left = 400
        self.width = 680
        self.height = 540
        icon_path = resource_path("icon.png")
        self.setWindowIcon(QtGui.QIcon(icon_path))

        self.InitUI()

    def InitUI(self):
        self.setWindowTitle(self.title) 
        self.setGeometry(self.top, self.left, self.width, self.height) 
        self.show()

App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
MalteHerrmann
  • 198
  • 1
  • 14

1 Answers1

4

The solution was to specifically add the image files to the .spec file then generate the .exe file using

$> pyinstaller myGUI.spec

Here is the relevant part of the .spec file:

a = Analysis(['myGUI.py'],
     ...,
     datas = [('myIcon.png', '.')],
     ...)
Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
MalteHerrmann
  • 198
  • 1
  • 14