I’m currently working on AppSnap, a Windows application which simplifies software installation. It originally started out as a console application but has since graduated into being GUI based as well. More on it when it gets released in a few days. For now, I’d like to spend some time talking about a few wxPython widgets that I use in AppSnap. wxPython has some decent documentation but it is still hard to figure out all aspects of a widget for a beginner. So here’s a few common widgets and their usage. I’ve also linked to the relevant pages in the wxPython API where appropriate so as to make further investigations easier.
Note that some of the examples below have been created to fit into the context of this post. AppSnap is laid out quite differently and I’ll go over that in a separate post.
Application Icon
I’m using Py2Exe to package my Python program into a Windows application. In order to associate an icon with my executable, I do the following in my Py2Exe setup file:-
)
I have a console as well as a GUI application. Both are associated with an icon file (lines in RED) so that the generated executable displays this icon when viewed in Windows Explorer. Also, note the line in BLUE. The icon is also being stored in the application package. This is to allow for the next step.
More details about icons in Py2Exe can be found here.
Window Icon
So our application executable shows up nicely with an icon in Windows Explorer. How about showing this icon in the title bar of our GUI application? The following lines show how this can be done:-
icon = wx.Icon(name=‘appsnap.ico’, type=wx.BITMAP_TYPE_ICO)
# Associate icon with top level frame
frame.SetIcon(icon)
We create an icon object from the file bundled in the previous step and associate it with our top level frame using the SetIcon() method. Pretty simple.
Dropdown box
On AppSnap, I use a dropdown box to list the available categories of applications such as Internet, Utilities and so forth. On selecting a specific category, only applications belonging to that category are listed on the GUI.
Dropdown boxes in wxPython are implemented using the wx.Choice widget. This wasn’t too obvious initially and I had to hunt it down. Creating a dropdown is trivial:-
Now, we can add some entries into the dropdown:-
list = [‘All’, ‘Internet’, ‘Utilities’]
dropdown.AppendItems(strings=list)
Now that our dropdown has something in it, a default item can be selected:-
Next, we bind a method to the dropdown. This method will get executed every time an item is selected:-
We can now perform some action in our OnChoice() method:-
def OnChoice(self, event):
Checklist
Checklists are essentially a combination of checkboxes and a listbox. A listbox can have multiple items. On clicking an item, the listbox action is executed and on checking an item, the checkbox action is executed. Hence, two different things can be done with each item.
In AppSnap, I use a checklist box to list all the applications for the category specified in the dropdown box. On selecting or checking an application, I show its details. Once the install or uninstall button is clicked, I perform that action on each checked application.
So let’s create a checklist box:-
checklist = wx.CheckListBox(parent=panel, pos=(5, 40))
Let’s add some items to it:-
list = [‘FileZilla’, ‘Firefox’, ‘Putty’, ‘SevenZip’]
checklist.InsertItems(items=list, pos=0)
We can bind two methods, one for the selection event and one for a check event:-
wx.EVT_LISTBOX(frame, checklist.GetId(), self.OnSelection)
# Bind method to check
wx.EVT_CHECKLISTBOX(frame, checklist.GetId(), self.OnCheck)
In AppSnap, both events do the same thing, i.e. display the application details. However, they obtain the application name differently since GetString() doesn’t seem to work for the check event:-
def OnSelection(self, event):
def OnCheck(self, event):
Remember the dropdown box above? The OnChoice() event is supposed to clear the checklist and populate it with the appropriate category of applications. Let’s modify the original OnChoice() event with something more functional. The method is simplified here but can be expanded as needed:-
def OnChoice(self, event):
# Clear checklist and repopulate with new list
checklist.Clear()
checklist.InsertItems(items=list, pos=0)
AppSnap will install or uninstall all checked applications once the corresponding button is pressed. Below is a peek at how the GetChecked() method gets the list of checked items for the checklist:-
def GetChecked(self):
Still to Come
That’s it for now. Please let me know if this approach is helpful. Meanwhile, I’ll be working on a post that goes over the following widgets:-
- wx.Button – A clickable button widget
- wx.StaticText – A widget to display text on the GUI
- wx.lib.hyperlink.HyperLinkCtrl – A widget to display a clickable hyperlink that opens in the default browser
- wx.Gauge – A progress bar widget
Filed under: Programming
Small typo, It should say:
checked.append(checklist.GetString(i))
Good catch! I’ve made the change.
Thanks for the tips! I always use slightly different code when binding events to controls:
(in some window, eg. frame)
self.Bind(wx.EVT_CHOICE, self.OnChoice, dropdown)
This takes away the need to work with clumsy IDs.
Good luck with your program!