XRC Cheat Sheet
Framework
wxButton
1 self.btn = XRCCTRL( self.frame, "ButtonName" ) # rarely needed
2 EVT_BUTTON( self.frame, XRCID( "ButtonName" ), self.OnClick )
1 def OnClick( self, evt ):
2 btn = evt.GetEventObject()
3 nam = evt.GetEventObject().GetName()
4 lbl = evt.GetLabel()
5 # do something
Use "-1" instead of XRCID("ButtonName") to direct all clicks to one handler.
For handle to button, add to OnInit:
1
wxChoice
1 self.choice = XRCCTRL( self.frame, "ChoiceName" )
2 EVT_CHOICE( XRCID( "ChoiceName" ), self.OnSelect )
1 def OnSelect( self, evt ):
2 sel_index = evt.GetSelection() # 0, 1, 2, ...
3 sel_str = evt.GetString() # "Apples", "Oranges", ...
4 choice = evt.GetEventObject() # <wxChoice>
5 # do something
wxMenuItem
An XRC object name which matches a stock window ID (wxID_NEW, wxID_OPEN, and so on) will be recognized, and the appropriate ID will be applied to the generated item. For example, the following XML defines the "New" menu item:
<object class="wxMenuItem" name="wxID_NEW">
<label>New</label>
<accel>Ctrl+N</accel>
</object>The Pythonic forms of the stock IDs (wx.ID_NEW) will not be recognized.
wxTextCtrl
1 self.txt = XRCCTRL( self.frame, "TextCtrlName" )
2
3 # need to add registrations for text change and enter key here...
Manipulating Text:
1 self.txt.SetValue( "Text for box." )
Introduction
(Intro at end of document on purpose.)
This cheat sheet exists to aleviate steps 2,3,4:
Use wxDesigner or wxGlade to create an XRC file.
Search on the wxPython wiki for "UsingXmlResources." (Generally, I've forgotten the name.)
Adapt UsingXmlResources' code to something that I like, to be a basic framework.
Scan for applicable pieces of UsingXmlResources, and search through the wxPython docs, to remember how to do the little things I want to do.
- Actually write the interesting code.
You are strongly invited to make this document more useful! If there's something that should be here, please add it!
General rule of thumb:
If it's a feature people use a lot, put it in here!
If it's a feature people use rarely, keep it out!
- Exercise good judgement in all other cases.
For example:
- Buttons are always clicked, so we put button clicking code in here.
- But buttons' labels rarely change, so we don't put that in here. Go look it up.
We want to get "that easy 90%", and just look up the remaining 10%.
