Python Programming from the Frontlines

Have you ever wondered if you could create a wxPython program using XML? Well, I never did either, but there is a way and its name is XRC. In fact, wxPython comes with an editor called XRCed that you can use to layout your GUI and generate the XML code with. In this article, we’ll give you a quick walk-through of XRC and how to use it to create a couple GUI skeletons. We will look at two examples that use only XRC controls and then a third that mixes in some additional non-XRC widgets.

Creating a Login Screen with XRC

A common dialog that we often see is a login dialog. I used XRCed to create the following XML code:

In the code above, we use xrc’s XmlResource method to open our XML file and load it in our program. Next, we use the resulting variable to load specific widgets from the file. In this case, we load just the frame by calling LoadFrame. Note that we passed None into the LoadFrame call. That first argument is the parent argument and since this frame shouldn’t have a parent, we passed it None. Lastly, we call the frame’s Show method so we can actually see our program. That’s all there is to it! Now let’s move onto something a little bit more complex.

Creating a Notebook with XRC

Creating a Notebook widget is a little trickier than just creating a frame. For one thing, when using a Notebook, you usually stick multiple panels on it. This can get confusing if your panels are complex. Thus, we’ll look at how to create a simple notebook and a slightly more complex version. Let’s start with the simple one first. If you want to follow along, open XRCed and see if you can copy the layout in the screenshot below:

XRCed layout of a Notebook

The trick to adding pages to your notebook in XRCed is that you need to select the child panel and choose the NotebookPage tab that appears on the right. In there you can set the labels for the tabs. Let’s take a look at the generated XML:

It’s pretty much the same as the code we saw previously. Note that we can embed sizer flags in the XML itself (e.g. wxALL|wx.EXPAND). That’s pretty neat! The code to load this notebook is almost exactly the same as the code we used for the login dialog earlier:

The only differences here are the names of the frame and the XRC file. Now let’s move on to our slightly more complex Notebook example. In this example, we will create a notebook XRC file and two Panel XRC files that we can use as tabs for the notebook. Our new Notebook’s XML is pretty much like the old one, so we’ll skip that. Instead, we’ll look at our Python code instead:

Here we just extract the frame, panel and notebook objects from the first XRC file and use those as our basis for adding other controls. Loading the other two panels is a cinch as we just do what we did to load the original panel. Then we add our new panels to the notebook using the familiar AddPage methodology. Once that’s done, we show the frame and we’re done! The second panel has an empty ListCtrl in it and when I first created it, I kept getting error messages because I forgot to set its style. Make sure you tell it that you want it to be in List, Report or one of its other modes or you’ll have issues too.

Adding Controls Outside of XRC

One of the issues with XRC is that it only supports a small subset of the widgets available. Fortunately, there are ways to “teach” XRC how to use new controls, but that is beyond the scope of this introductory article. Instead, I’ll show you how to add the controls outside of XRC. The concept is the same as using normal widgets, so it’s really easy to understand. In fact, we’re going to take the second notebook example and add a PlateButton to it:

Notice that all we had to do was take the XRC Panel widget and make it the PlateButton’s parent. Then we added the notebook and the button to a vertically oriented sizer. Now we know how to combine normal widgets with XRC widgets in our applications. I hope you’ve learned a lot from this article and will find it helpful in your own work.

Further Reading

Downloads

Thanks Mike.Is there a noticeable performance hit having the controls rendered from xml at runtime?

driscollis

Not that I've noticed. Admittedly, I haven't used a complex XRC file either.

– Mike

driscollis

Hi Joaquin,

Good question! Two that I can think of off the top of my head are as follows:

1) Users with XML experience can create or edit GUIs without learning wxPython2) Putting the layout stuff in XRC can help you conform to the MVC model better (the idea of separating your model and your controller)

I'm sure there are other good reasons, but I don't know what they are.

– Mike

MrMe

Mike- if you have time- would you add a tutorial which contains several levels of nested sizers, using XRC?

I usually use Box Sizers mostly, and try to avoid all the other types. They just seem complicated and not documented clearly. But whatever you decide to serve the greater good.