Introduction

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"
GUI_MAINFRAME_NAME = "FRAME1"
class MyApp( wx.App ):
    def OnInit( self ):
        self.res   = xrc.XmlResource( GUI_FILENAME )
        self.frame = self.res.LoadFrame( None, GUI_MAINFRAME_NAME )
        self.frame.Show(1)
        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)
        size=sizer.GetMinSize()
        self.frame.SetMinSize(size)
        self.frame.Fit()
if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()

and t.xrc:

<?xml version="1.0" encoding="cp1252"?>
<resource>
  <object class="wxFrame" name="FRAME1">
    <title>Test</title>
    <centered>1</centered>
    <object class="wxBoxSizer">
      <orient>wxVERTICAL</orient>
      <object class="sizeritem">
        <object class="wxToggleButton" name="showControls">
          <label>Show Controls</label>
          <checked>1</checked>
        </object>
      </object>
      <object class="sizeritem">
        <object class="wxPanel" name="controlPanel">
          <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="sizeritem">
              <object class="wxStaticText" name="theLabel">
                <label>Label:</label>
              </object>
            </object>
            <object class="sizeritem">
              <object class="wxTextCtrl" name="theTextControl"/>
              <option>1</option>
            </object>
          </object>
        </object>
      </object>
    </object>
  </object>
</resource>

Comments

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.