Adding an icon to your application's title bar as a way of branding the application that will help to set it apart and distinguish it from the other applications running on the desktop. This recipe will show how easy it is to add an icon to a Frame.
Here we will create a Frame
subclass that loads an image file from the hard disk and displays it on its title bar:
class MyFrame(wx.Frame): def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="MyFrame"): super(MyFrame, self).__init__(parent, id, title, pos, size, style, name) # Attributes self.panel = wx.Panel(self) # Setup path = os.path.abspath("./face-monkey.png") icon = wx.Icon(path, wx.BITMAP_TYPE_PNG) self.SetIcon(icon)
Displaying this Frame subclass will result in a window like the following. Comparing this to the one in the Main Frame recipe, you can see the new icon to the left of the title:
In this recipe we have a small (16x16) image of a monkey that we want to show in the title bar of the Frame
. For simplicity, this image is located in the same directory as our script and we load it using a relative path. The Frame
requires an icon instead of a Bitmap
, so we have to use an Icon
to load our image into memory. After loading the image, all that is left is to call the Frame's SetIcon
method in order to set the Icon for the Frame
.