XRC Cheat Sheet

Framework

wxButton

OnInit:

   1 self.btn = XRCCTRL( self.frame, "ButtonName" ) # rarely needed
   2 EVT_BUTTON( self.frame, XRCID( "ButtonName" ), self.OnClick )

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

OnInit:

   1 self.choice = XRCCTRL( self.frame, "ChoiceName" )
   2 EVT_CHOICE( XRCID( "ChoiceName" ), self.OnSelect )

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

OnInit:

   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:

  1. Use wxDesigner or wxGlade to create an XRC file.

  2. Search on the wxPython wiki for "UsingXmlResources." (Generally, I've forgotten the name.)

  3. Adapt UsingXmlResources' code to something that I like, to be a basic framework.

  4. Scan for applicable pieces of UsingXmlResources, and search through the wxPython docs, to remember how to do the little things I want to do.

  5. 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:

  1. If it's a feature people use a lot, put it in here!

  2. If it's a feature people use rarely, keep it out!

  3. Exercise good judgement in all other cases.

For example:

We want to get "that easy 90%", and just look up the remaining 10%.

XrcCheatSheet (last edited 2009-11-05 22:23:38 by D-128-95-103-211)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.