Introduction

This topic is about how to add a named border to a group of widgets.

What Objects are Involved

Two wxPython classes are used: wxStaticBox and wxStaticBoxSizer.

Process Overview

I found these two classes to be hard to work with, and require a slightly different coding style than the other containers. So, I wrote a subclass of wxPanel that encapsulates how they work, make the application programmer's job easier. Here's an example of how to use my class (NamedPanel). This code creates a panel with a name and border around it. The panel contains a single button.

   1 namedPanel = NamedPanel(frame, 'My name goes here')
   2 aButton    = wxButton(namedPanel, -1, 'aButton')
   3 namedPanel.setup()

Special Concerns

The coding for NamedPanel is very simple: it expects just one child component. So, if you want to put multiple components in the named panel, first lay them out in a seperate wxPanel and then add that one wxPanel.

Source Code: NamedPanel.py

   1 import wx
   2 
   3 class NamedPanel(wx.Panel):
   4     """
   5     A wx.Panel subclass that has a border and a visible name.
   6     Here's how it's used:
   7     
   8       namedPanel = NamedPanel(frame, 'My name goes here')
   9       aButton    = wx.Button(namedPanel, -1, 'aButton')
  10       namedPanel.setup()
  11 
  12     I hope to figure out how to avoid the need to call setup()
  13     explicitly.
  14     
  15     Author: Robb Shecter (robb@acm.org)
  16     """
  17 
  18     def __init__(self, parent, name='No Name'):
  19         "Instantiate a new NamedPanel with the given name."
  20         wx.Panel.__init__(self, parent, -1)
  21         group      = wx.StaticBox(self, -1, name)
  22         if "__WXMAC__" in wx.PlatformInfo:
  23             self.box   = wx.StaticBoxSizer(group)
  24         else:
  25             self.box   = wx.StaticBoxSizer(group, wx.VERTICAL)
  26         
  27         self.isSetup = 0
  28 
  29     def setup(self):
  30         "Prepare the UI. Required before the panel is shown."
  31         self.__setChildPanel(self.GetChildren()[1])
  32 
  33     def __setChildPanel(self, aPanel):
  34         "Specify the panel to display inside the named border"
  35         if self.isSetup:
  36             raise 'NamedPanel.__setChildPanel() was called twice'
  37         self.isSetup = 1
  38         if "__WXMAC__" in wx.PlatformInfo:
  39             self.box.Add(aPanel, 1, 4)
  40         else:
  41             self.box.Add(aPanel, 1, wx.EXPAND|wx.ALL, 4)
  42             
  43         self.SetAutoLayout(True)
  44         self.SetSizer(self.box)
  45         self.box.Fit(self)
  46         self.box.SetSizeHints(self)
  47 
  48 if __name__ == '__main__':
  49     """Create a window with a NamedPanel containing a wx.Button"""
  50     app = wx.PySimpleApp()
  51     frame = wx.Frame(None, -1, 'NamedPanel Test', size=(300,300))
  52 
  53     namedPanel = NamedPanel(frame, 'My name goes here')
  54     aButton    = wx.Button(namedPanel, -1, 'aButton')
  55     namedPanel.setup()
  56 
  57     frame.Show(1)
  58     app.MainLoop()

Comments

Page originally created by RobbShecter, robb@acm.org

Page updated to use 2.5.3.1 wx. syntax and tested to work on OSX 10.3.7 with Python 2.4 (without the check for WXMAC, the text for the button appears in the wrong place when the window is drawn or resized....can someone please fix this!) by JonStevens, jon@latchkey.com

The following code from UsingSizers should be helpful here:

   1 wxStaticBoxSizer( wxStaticBox( parent_window, id, "label" ), wxHORIZONTAL )

-- LionKimbro 2003-08-17 19:41:18

WorkingWithStaticBoxes (last edited 2008-03-11 10:50:28 by localhost)

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