I received a question about how to put an image in my frame’s toolbar on Windows. As it is, the toolbar just uses a generic icon. There are three ways that I am aware of. The first is to get an embedded image out of an executable. The second is to take some image you have and just embed it. The last way is to take your image and turn it into a Python file that can be imported. I’m sure you could also mess with PIL or maybe even use the paint handler, but I don’t know that stuff.
I’ll talk about getting an embedded image from an executable first. It’s actually pretty simple. Here’s the basic idea:
import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, 'Image Extractor') # Add a panel so it looks the correct on all platforms self.panel = wx.Panel(self, wx.ID_ANY) loc = wx.IconLocation(r'L:\Python25\python.exe', 0) self.SetIcon(wx.IconFromLocation(loc)) # Run the program if __name__ == '__main__': app = wx.PySimpleApp() frame = MyForm().Show() app.MainLoop()
In this example, I’m grabbing the Python 2.5 icon out of the python.exe using the following line:
loc = wx.IconLocation(r'L:\Python25\python.exe', 0)
Then I set the frame’s icon using SetIcon(). Notice that I also need to use wx.IconFromLocation(loc), which I nest in the SetIcon() call.
Next I’ll talk about just using any image you have on hand. The only difference between this code and the code above is that I’ve gotten rid of the calls to wx.IconLocation and wx.IconFromLocation and added a wx.Icon object. The wx.Icon object just needs a path to the icon and the wx.BITMAP_TYPE_ICO flag. See the full code below:
import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, 'Image Extractor') # Add a panel so it looks the correct on all platforms self.panel = wx.Panel(self, wx.ID_ANY) ico = wx.Icon('py.ico', wx.BITMAP_TYPE_ICO) self.SetIcon(ico) # Run the program if __name__ == '__main__': app = wx.PySimpleApp() frame = MyForm().Show() app.MainLoop()
The final way I would do this may be the best. In it, I take an icon or image and turn it into a python file using wx’s img2py utility. Why might this be the best? Because by embedding the image file in a python file, you simplify the process of distributing your application with py2exe. At least, that’s been my experience.
On my PC, the img2py utility can be found here:
C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\tools>
Adjust as needed for your setup. Open a command window and navigate to this directory. Then type the following command:
python img2py.py -i path/to/your/icon.ico myIcon.py
The first argument to img2py.py is the -i, which tells the utility that you’re embedding an icon. Next is the path to the icon file. Finally, you give the name of the file that you want img2py to create (i.e. embed the icon into). Now, copy the python file you just created over to the folder that contains your wxPython script so it can import it (or you can just copy the code out of the Python file into the text of the application you’re creating).
I’m going to import it for this example. To get the icon, you call the getIcon() method of the icon file I imported. Check out the code to see what I’m doing:
import wx import myIcon class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, 'Image Extractor') # Add a panel so it looks the correct on all platforms self.panel = wx.Panel(self, wx.ID_ANY) ico = myIcon.getIcon() self.SetIcon(ico) # Run the program if __name__ == '__main__': app = wx.PySimpleApp() frame = MyForm().Show() app.MainLoop()
Hopefully this tutorial has helped you learn how to use your icon in your application. Remember, you can use these techniques for any image you want to insert; not just for the title bar icon, but for any static image you’d use in your application, such as a taskbar icon or a toolbar icon. Good luck!
Further Reading:
wxPython Wiki – Flashing Taskbar Icon
Downloads:
Excellent tutorial!
good job bro;)
good job bro;)
Excellent tutorial
Excellent tutorial
Simple, short, straightforward, clean and clear. That’s way to go! Congratulations!
Regards,
Neven
Pingback: Type on android using adb, python gui | Sandip Tiwari