I had trouble finding out how to resize a frame when controls were shown or hidden. This documents the results of my search of the mailing lists and experiments.

What Objects are Involved

wxPanel, wxSizer

Process Overview

The code displays a frame with a toggle button and a pane below it containing some controls. Toggling the button shows and hides the control pane, resizing the frame as appropriate.

Code Sample

The skeleton of this code is derived from the sample code at XrcStartingPoints.

import wx
from wx import xrc
GUI_FILENAME       = "t.xrc"
class MyApp( wx.App ):
    def OnInit( self ):
        self.res   = xrc.XmlResource( GUI_FILENAME )
        self.frame = self.res.LoadFrame( None, GUI_MAINFRAME_NAME )
        self.showControls = xrc.XRCCTRL(self.frame, 'showControls')
        self.controlPanel = xrc.XRCCTRL(self.frame, 'controlPanel')
        self.Bind( wx.EVT_TOGGLEBUTTON, self.OnChange, self.showControls)
        return 1
    def OnChange(self, event):
        sizer = self.frame.GetSizer()
        sizer.Show( self.controlPanel, show=self.showControls.GetValue(), recursive=True)
if __name__ == '__main__':
    app = MyApp(0)

and t.xrc:

<?xml version="1.0" encoding="cp1252"?>
  <object class="wxFrame" name="FRAME1">
    <object class="wxBoxSizer">
      <object class="sizeritem">
        <object class="wxToggleButton" name="showControls">
          <label>Show Controls</label>
      <object class="sizeritem">
        <object class="wxPanel" name="controlPanel">
          <object class="wxBoxSizer">
            <object class="sizeritem">
              <object class="wxStaticText" name="theLabel">
            <object class="sizeritem">
              <object class="wxTextCtrl" name="theTextControl"/>


I don't know if this is the best way to do this, but the code is clean and it works. Please add any comments.

ResizingFramesUsingSizers (last edited 2008-03-11 10:50:22 by localhost)

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