Adding Custom Desktop and Taskbar Icons to you Python Executable

After going through the process of setting up pyinstaller to stop throwing false virus warnings, you probably want your fancy new desktop app to have its own custom icon.  There is a special method required for each of these icons to work as you expect.

Normal windows programs are a single .EXE file with a custom icon. When you open them, there’s a GUI and the taskbar and window icons are the same. To accomplish this, I use pySimpleGUI to design the GUI. This is simple to install and there are several version available such as pysimpleguiQt. Install this in the command line with:

pip install pysimplegui

I actually cheat a little bit here. You can do everything on the command line, but pysimplegui has a great GUI for this that simplifies things called pysimplegui-exemaker.

Step 1: Create a folder structure for your python project.

There’s likely easier ways to do this, but I typically start my projects by visiting github.com and creating a new repository on my account. Then I open github desktop and clone that directory to my computer. This gives me a correct structure for keeping track of any changes in that folder. I’ll create my main python file in this folder and edit the README file with a description and my project goals.

I create a folder to hold my assets named something like “assets” or “img” where I will save my custom icon file. This can be edited later, but I like having something here so I know from the beginning that it is set up correctly.

Step 2. Get or create a .ico file.

This is the image of your icon. They are really easy to find free ones online or you can make one from scratch using a free online editor or some image editors gimp.

Step 3. Setup Relative Filepaths

I add the following to the top of my main python script based on advice from arcade academy which allows relative filepaths for pyinstaller:

if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
os.chdir(sys._MEIPASS)

Step 4: Setting the Window Icons:

To get my custom icon in the pysimpleGUI you have to use a fill absolute path when you create your windows and all popup windows. I typically just create a global variable with the absolute path to the location of the icon.

program_icon = C:\Users\ALaptop\Documents\GitHub\myProjectFolder\assets\icon.ico
window = sg.Window('Program Title Goes Here', layout, icon =program_icon)
filename = sg.popup_get_file('Select a MPA file', no_window=True, file_types=(("myPythonApp Files", "*.mpa"), ("Text files", "*.txt"), ("All Files", "*")), icon =program_icon) 

When you run this code as a script (in VScode for example) you should see the custom icon at the top left of all the program’s windows. But your taskbar will still be a generic python icon.  You won’t see the icon in the windows taskbar until you create and run an executable using pysimplegui’s exemaker or pyinstaller. Simply running the script from the command line will NOT show the correct icon in the taskbar.

Step 5: Setting Desktop and Taskbar icons

Pyinstaller can set up a template for us to fill out for our project so it will be able to bring in all the external files we want (like icons). Simply run the following command in the command line to generate the template:

pyinstaller --onefile --noconsole .\my-Program.py
This creates a new file called “my-Program.spec” in the same folder as your code. Open this in a text editor and it’ll look something like this:
# -*- mode: python -*-

block_cipher = None

a = Analysis(['my-Program.py'],
pathex=['C:\\Users\\ALaptop\\Documents\\GitHub\\myProjectFolder'],
binaries=[],
datas=[],
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='my-Program',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False )

Step 6: Customizing the .spec file

Once the .spec file is created, you’ll want to edit it. In the Analysis section in datas I put the links to any assets the program will need such as images, sounds, etc. If I only have images in a subfolder named “img” it would be

datas=[( 'img/*png', 'img' )],

The first parenthesis is where the files are currently stored, and the second is the name of the folder inside the temporary folder that pyinstaller uses when the exe runs. Remember how I said Pyinstaller creates a self-extracting zip file? Here’s where that comes into play. You are telling pyinstaller where you stored your assets relative to your project’s folder, and then telling it where it can put these files later. This path is important especially if you used relative links in your python code. If your EXE can’t find the files it needs, it’ll crash giving you a popup stating “Failed to execute script my-Program”. This can be confusing to debug if you don’t know how to see what the executable is looking for. Also, this expects that you pasted the lines from Step 3 above into your code which sets up relative filepaths.

To understand and to debug this, you have to remember that the EXE pyinstaller creates is a zip file that is made up of a bootloader (which can actually run your python code), your python script, any libraries your script uses auto extracts a folder, and any other files needed to run your script (images, audio, etc). When you run the executable, it actually decompresses all this and sticks it in a folder in your C:\Users\<username>\AppData\Local\Temp in a directory named “_MEIxxxxx” where the ‘x’s are a random number. This folder exists only while the executable (or the error popup) is opened. As soon as you close it, this folder is deleted. You need to visit this folder to debug issues. To debug, you’ll need to pay close attention to the paths in the spec file and your code’s paths and then comparing them to the location of assets inside this temporary _MEIxxxxx folder.

For instance, in the datas entry above, the second set of quotes denotes the name of the folder INSIDE the temporary folder. That’s where pyinstaller is placing the PNGs I refer to in that line. If my app keeps crashing for some reason, I can make sure that these files exist in the correct folder in the C:\Users\<username>\AppData\Local\Temp\_MEIxxxxx\img folder.

Add the absolute path to your custom icon to the .spec file. This is added to the “EXE” section as part of the list of settings. I also added “console=False” so it would not open a console while executing.

console=False , icon='C:\\Users\\ALaptop\\Documents\\GitHub\\myProjectFolder\img\\icon.ico')

You can see my full spec file below. Once you’ve saved the spec file changes, you will issue the same command as before, but use the .spec file instead of the python file.

pyinstaller --onefile .\my-Program.spec

Just for good measure, I also put the fullpath in the pysintaller command as well sometimes:

pyinstaller --onefile --noconsole --icon "C:\\Users\\ALaptop\\Documents\\GitHub\\myProjectFolder\\icon.ico" --add-data "*png;." .\my-Program.spec

Here’s the full final .spec file I would use:

 # -*- mode: python -*-

block_cipher = None


a = Analysis(['my-Program.py'],
             pathex=['C:\\Users\\ALaptop\\Documents\\GitHub\\myProjectFolder'],
             binaries=[],
             datas=[( 'img/*png', 'img' )],
             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='my-Program',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False , icon='C:\\Users\\ALaptop\\Documents\\GitHub\\myProjectFolder\\img\\icon.ico')

 

Program Icon (Desktop Icon):

This will be the icon users see and doubleclick on their desktops to open your app.

After running pysimplegui’s exemaker one time for your project as normal, it will create a .spec file template for you.  Once the .spec file is created, you have to put the absolute path to the icon you want to use in that .spec file. For instance, I had my icon in a folder called “img” inside my python’s project folder. Paste the path at the bottom of the EXE section like this:

console=False , icon='C:\\Users\\ALaptop\\Documents\\GitHub\\myPythonApp\\img\\icon.ico')

Just for good measure, I also put the fullpath in the pysintaller command as well:

pyinstaller --onefile --noconsole --icon "C:\\Users\\ALaptop\\Documents\\GitHub\\myPythonApp\\icon.ico" --add-data "*png;." .\myPythonApp.spec

Window and Taskbar Icon:

This is set in pysimgplegui when you create any window (even popups can be given a different icons). The only way this works is if you code the full absolute path in the python script, but you won’t see it in the taskbar until you create and run an executable using pysimplegui’s exemaker or pyinstaller. Simply running the script from the command line will NOT show the correct icon in the taskbar.

program_icon = 'C:/Users/ALaptop/Documents/GitHub/myPythonApp/img/icon.ico'

filename = sg.popup_get_file('Select a MPA file', no_window=True, file_types=(("myPythonApp Files", "*.mpa"), ("Text files", "*.txt"), ("All Files", "*")), icon =program_icon)

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.