<> == 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: wx``Static``Box and wx``Static``Box``Sizer. == 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 (Named``Panel). This code creates a panel with a name and border around it. The panel contains a single button. {{{ #!python namedPanel = NamedPanel(frame, 'My name goes here') aButton = wxButton(namedPanel, -1, 'aButton') namedPanel.setup() }}} == Special Concerns == The coding for Named``Panel 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 == {{{ #!python import wx class NamedPanel(wx.Panel): """ A wx.Panel subclass that has a border and a visible name. Here's how it's used: namedPanel = NamedPanel(frame, 'My name goes here') aButton = wx.Button(namedPanel, -1, 'aButton') namedPanel.setup() I hope to figure out how to avoid the need to call setup() explicitly. Author: Robb Shecter (robb@acm.org) """ def __init__(self, parent, name='No Name'): "Instantiate a new NamedPanel with the given name." wx.Panel.__init__(self, parent, -1) group = wx.StaticBox(self, -1, name) if "__WXMAC__" in wx.PlatformInfo: self.box = wx.StaticBoxSizer(group) else: self.box = wx.StaticBoxSizer(group, wx.VERTICAL) self.isSetup = 0 def setup(self): "Prepare the UI. Required before the panel is shown." self.__setChildPanel(self.GetChildren()[1]) def __setChildPanel(self, aPanel): "Specify the panel to display inside the named border" if self.isSetup: raise 'NamedPanel.__setChildPanel() was called twice' self.isSetup = 1 if "__WXMAC__" in wx.PlatformInfo: self.box.Add(aPanel, 1, 4) else: self.box.Add(aPanel, 1, wx.EXPAND|wx.ALL, 4) self.SetAutoLayout(True) self.SetSizer(self.box) self.box.Fit(self) self.box.SetSizeHints(self) if __name__ == '__main__': """Create a window with a NamedPanel containing a wx.Button""" app = wx.PySimpleApp() frame = wx.Frame(None, -1, 'NamedPanel Test', size=(300,300)) namedPanel = NamedPanel(frame, 'My name goes here') aButton = wx.Button(namedPanel, -1, 'aButton') namedPanel.setup() frame.Show(1) 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: {{{ #!python wxStaticBoxSizer( wxStaticBox( parent_window, id, "label" ), wxHORIZONTAL ) }}} -- LionKimbro <>