Custom Derived Classes for wxPython XRC resources

First of all, this isn't a topic that is bran new or which requires new documentation. I have learned a few quirks about the process for creating custom controls, panels, frames, and other elements with XRC files in wxPython and I thought I'd write up a little post.

Let me point you to the two most important resources. The wxPython wiki has pretty much all you need to now to get started. First is the TwoStageCreation process that the XRC implementation uses to create classes. The second is the UsingXmlResources page.
There is also the XRCTutorial. But you should be past that if you're trying to create custom controls.

Here is a summary of the basic process. First, create your XRC file. Here is a simple sample I generated with DialogBlocks.


  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <resource version="" xmlns="">
  4. <object class="wxFrame" name="ID_WXFRAME" subclass="wxtest.MyFrame">
  6. <size>400,300</size>
  7. <title>wxTest</title>
  8. <object class="wxPanel" name="ID_PANEL">
  9. <style>wxSUNKEN_BORDER|wxTAB_TRAVERSAL</style>
  10. <object class="wxBoxSizer">
  11. <orient>wxHORIZONTAL</orient>
  12. <object class="spacer">
  13. <flag>wxALIGN_CENTER_VERTICAL|wxALL</flag>
  14. <border>5</border>
  15. <option>1</option>
  16. <size>5,5</size>
  17. </object>
  18. <object class="sizeritem">
  19. <flag>wxALIGN_CENTER_VERTICAL|wxALL</flag>
  20. <border>5</border>
  21. <object class="wxButton" name="ID_BUTTON">
  22. <label>Test Button</label>
  23. </object>
  24. </object>
  25. <object class="spacer">
  26. <flag>wxALIGN_CENTER_VERTICAL|wxALL</flag>
  27. <border>5</border>
  28. <option>1</option>
  29. <size>5,5</size>
  30. </object>
  31. </object>
  32. </object>
  33. </object>
  34. </resource>

Notice at the top of the file, the wxFrame object has the attribute subclass="wxtest.MyFrame". This tells XRC that when instantiating this class, use my subclass of wxFrame instead of wxFrame itself.

Here is the python code to load the frame:

  3. import wx
  4. from wx import xrc
  6. import logging as log
  7. log.basicConfig ( format='%(message)s', level=log.DEBUG )
  10. class MyFrame(wx.Frame):
  12. def __init__(self):
  13. log.debug ( "__init__")
  14. f=wx.PreFrame()
  15. self.PostCreate(f)
  16. self.Bind( wx.EVT_WINDOW_CREATE , self.OnCreate)
  17. def OnCreate(self,evt):
  18. log.debug ( "OnCreate" )
  19. self.Unbind ( wx.EVT_WINDOW_CREATE )
  20. wx.CallAfter(self.__PostInit)
  21. evt.Skip()
  22. return True
  23. def __PostInit(self):
  24. log.debug ( "__PostInit" )
  25. self.Bind ( wx.EVT_BUTTON, self.OnButton, id=xrc.XRCID ( "ID_BUTTON" ) )
  26. def OnButton(self,evt):
  27. log.debug ( "Button Pressed" )
  30. if __name__ == '__main__':
  31. app=wx.PySimpleApp(redirect=False)
  32. res=xrc.XmlResource ( 'wxtest.xrc' )
  33. frame=res.LoadFrame( None, "ID_WXFRAME" )
  34. frame.Show()
  35. app.MainLoop()

You might wonder why bind the OnCreate method to the WINDOW_CREATE event instead of simply doing your initialization in the init method. The answer is that 1) the window isn't really all the way created at the __init__ stage and 2), it doesn't get all the way created until after the events start being processed. Why use wx.CallAfter and a __PostInit method then? This seems to not be absolutely necessary if you're creating a frame or panel as part of your main program. If you are creating an XRC control withing an event handler, e.g., you have a button that is used to create a dialog that is defined in an XRC file, the control still isn't all the way loaded until after the Create Event. I'm not sure why you can't just use wx.CallAfter in your __init__. I tried that and found it to work, but the documented procedure is to use both OnCreate and PostInit.

Important summary note: Child controls obtained with xrc.XRCCTRL aren't available to be loaded until the __PostInit method.

Now, having the basic example up and running, it's time to add a little customization. I found it annoying to write the __init__, OnCreate, and __PostInit methods for each of my custom controls. You can create a parent class to do this.

  2. class XrcControl:
  3. def __init__(self):
  4. log.debug ( "__init__")
  5. self.Bind( wx.EVT_WINDOW_CREATE , self.OnCreate)
  6. def OnCreate(self,evt):
  7. log.debug ( "OnCreate" )
  8. self.Unbind ( wx.EVT_WINDOW_CREATE )
  9. wx.CallAfter(self._PostInit)
  10. evt.Skip()
  11. return True
  12. def _PostInit(self):
  13. raise RuntimeError ( "Extend this method." )
  15. class XrcFrame(wx.Frame, XrcControl):
  16. def __init__(self):
  17. f=wx.PreFrame()
  18. self.PostCreate(f)
  19. XrcControl.__init__(self)

It's pretty easy to see how you could create Xrc elements for Panels and other widgets. (I think Python needs templates.) Anyway, MyFrame is now a bit smaller:

  2. class MyFrame(XrcFrame):
  3. def __init__(self):
  4. XrcFrame.__init__(self)
  5. def _PostInit(self):
  6. log.debug ( "__PostInit" )
  7. self.Bind ( wx.EVT_BUTTON, self.OnButton, id=xrc.XRCID ( "ID_BUTTON" ) )
  8. def OnButton(self,evt):
  9. log.debug ( "Button Pressed" )


* You can't access child elements until OnCreate for controls that are loaded outside of an event handler and PostInit (call it whatever you want) for controls that are loaded from within an event handler.
* You can use Python's very flexible style to put lots of the code for creating XRC controls into base classes.

  1. No Comments