Search code examples
pythonpyqtpyinstaller

python pyinstaller pyqt add custom icons to the interface


I have searched everywhere, I can't figure out how to add my own icons to the interface.

I don't need to know how to change the application icon or the title bar, what I need is for PyInstaller to add to the executable the images I use for buttons in the GUI once compiled.

The best I found was this, but it doesn't work:

pyinstaller --clean --onefile --noconsole --icon="media/icon.png" -i="media/icon.png" --add-data="media/*;." main.py

That my icons are shown in the buttons, but not the default PyQt icons, but the customized ones.


Solution

  • With the --one-file flag, when the app executable is ran, pyinstaller unpacks all the files into a temporary directory, this temporary directory is then passed to sys._MEIPASS. So in order to access the icon files in your media folder the base directory should start from sys._MEIPASS.

    import sys
    from os import path
    # If we're running the app as an executable or as a python script
    if getattr(sys, "frozen", False):  
        # If it's an executable set the base directory to sys._MEIPASS
        base_directory = sys._MEIPASS
    else:
        # Otherwise set it as the folder containing the main script
        base_directory = path.dirname(path.realpath("__file__"))
    
    media_folder = path.join(base_directory, "media")
    button_icon = path.join(media_folder, "button.png")
    

    Additionally you don't have to specify the media/* or use ; as of the latest version of pyinstaller you simply say --add-data=media:media.