How to set a Custom Windows Icon with Py2Exe

By default, we see the below taskbar icon when running a sample application (built with py2exe):

a default application icon on windows, not custom

As shown on the py2exe site, we can add the following line:

"icon_resources": [(1, "myicon.ico")]

to our build_exe.py script. The code is now:

from distutils.core import setup
import py2exe
from cefpython3 import cefpython
import wx
import time
import re
import uuid
import platform
import inspect
import struct
import urllib
setup(
    windows=[{
        'script': 'wxpython.py',
        "icon_resources": [(1, "myicon.ico")]
    }]
)

The icon file I used was created using IcoFX (not free, but very handy) as detailed here. The result is:

the custom application icon with the windows app open

To set the icon in the upper-left portion of the application window, you can add the following code (documented here) to the code file (wxPython.py, in our example):

_icon = wx.Icon('app.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(_icon)

a custom little icon in the upper left of the app window

Comments

Leave a comment

What color are brown eyes? (spam prevention)
Submit
Code under MIT License unless otherwise indicated.
© 2020, Downranked, LLC.