Monday, September 24, 2007

Tips: Toolbar icons in wxPython

I have started playing around with wxPython, a great toolkit for making clean, cross-platform, flexible GUIs in Python. While there are a lot of great tutorials out there on how to get started making apps with wxPython, having The Book has been a definite boon.

While going through the basic exercises and starting to make my own GUIs, I kept running into an issue that got more and more annoying: toolbar icons. I wanted to have a toolbar, with some basic icons, for actions like New, Open, etc. In the book, Rappin and Dunn give an example of using toolbar icons (p49). The pertinent part:
import wx
import images
toolbar = self.CreateToolbar()
toolbar.AddSimpleTool(wx.NewId(), images.getNewBitMap(), "New", "Long help for 'new'")
This in no way works. When I ran it on Ubuntu (7.04), I got:

ImportError: No module named images

After some Googling, I found this is a common problem for beginners. Lots of related example code will be similar to the above, but specify something like "stock_new.png" for the image. To get all these to work, you actually need to download some images, and specify those. Seems obvious after the fact, but it seemed quite possible to me starting out there there would be a built-in facility to grab OS-specific icons for basic tasks such as New and Load.

A great source of good-looking free icons for desktop applications is the Tango Desktop Project. Sometimes that page is down or takes a very long time to load. If so, you can download the .zip on this page. Every icon you will likely want for a toolbar may be found in "actions", once you extract the Tango folder and select an icon size to use. I found it helpful to place the icons I wanted in a "resources" folder in the folder holding my application, and use them with code such as:
    toolbar = self.CreateToolBar()
toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/document-new.png',
wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'New', 'Long help for New')
toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/document-open.png',
wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Open', 'Long help for Open')
toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/document-save.png',
wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Save', 'Long help for Save')
That will result in something like the following (Menubar added for positional context):


Blogger clin said...

Thanks for posting this. I'm at this example right now and, surprise, it doesn't work.

December 3, 2007 at 2:08 PM  
Blogger clin said...

The "images" file that is imported is not the one in the demo folder. You can, however, download the entire source code for the book from Once you unzip, the files are broken down into chapters. In chapter 2, there's Drop that in the directory you are doing your example in.

Your point is still valid (these icons should be part of wxPython).

December 3, 2007 at 2:32 PM  
Blogger by Immortal Curiosity said...

Hey cool, had not checked out the downloadable source code. Good to know!

Even outside of the book, I was surprised no one had put together a module to provide these really common icons. Oh well, maybe I should do it instead of complaining:-)

December 3, 2007 at 4:34 PM  
Anonymous Anonymous said...

Check out wxPython's wx.ArtProvider. I'm still learning wxPython myself but it appears that it provides access to some of the platform defined icons.

September 28, 2008 at 7:22 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home