Include Pystray Icon when using PyInstaller

Viewed 221

I am using PyInstaller to create a single file exe for my python application. I am also using Pystray to create a tray icon with a menu on it.

When running the script directly it works, but when I use pyinstaller I get an error that the "icon.png" cannot be found. The file is in the root of my project directory.

I have tried --add-data "icon.png;icon.png" and --add-data "icon.png;." neither of those helped.

I also placed a copy of the icon.png file in pretty much every sub directory there was just to see if it would try to reference it from somewhere else.

Any ideas how to include the png file

Python Script:

from pystray import MenuItem as item
import pystray
from PIL import Image

def openConfig():
    webbrowser.open('http://localhost:5000', new=2)
def openAbout():
    webbrowser.open('http://localhost:5000/about', new=2)
def closeApp():
    os._exit(0)

image = Image.open("icon.png")
menu = (item('Configuration', openConfig), item('About', openAbout), item('Quit', closeApp))
icon = pystray.Icon("name", image, "MyApp Name", menu)

icon.run()

Command:

pyinstaller -w -F MyApp.py

Or:

pyinstall -w -F --add-data "icon.png;icon.png" MyApp.py

The Error: enter image description here

1 Answers

I finally figured it out. I put my image in a folder called static, and in the python I changed the code to the following:

icon_folder = os.path.join(sys._MEIPASS, 'static')
image = Image.open(icon_folder+"\icon.png")

Then in the command I ran:

pyinstaller --onefile --add-data "static;static" --add-data myApp.py
Related