StaticBoxSizer Question

Hi,
I’m working on my first wxPython project, so be kind:-). I hacked up the code snippet below from the StaticBox portion of the demo. I’m having trouble getting the static text (t) to display properly. If I set it’s parent to self.panel, it displays on top of the StaticBox label. If I set it’s parent to self, it doesn’t display at all. What am I missing?

Thanks,
Patrick

···

class TestPanel(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1)

    sizer = wx.GridBagSizer(hgap=5, vgap=5)
    self.panel = wx.Panel(self)
    sizer.Add(self.panel, pos=(0,0), flag=wx.EXPAND)

    box = wx.StaticBox(self.panel, -1, "Label Text Here")
    sizer.Add(box, pos=(0,0))

    bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
    box.SetSizer(bsizer)

    t = wx.StaticText(self, -1, "Some other text here")
    bsizer.Add(t, proportion=1, flag=wx.ALL, border=10)
   
    bsizer.Fit(box)       
    self.SetSizer(sizer)
    self.Fit()

Hi,

Hi,
I'm working on my first wxPython project, so be kind:-). I hacked up the
code snippet below from the StaticBox portion of the demo. I'm having
trouble getting the static text (t) to display properly. If I set it's
parent to self.panel, it displays on top of the StaticBox label. If I set
it's parent to self, it doesn't display at all. What am I missing?

There are a few (mostly hierarchical) issues here,

---------------------------------------------------------------------------------------------------

class TestPanel(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)

    sizer = wx\.GridBagSizer\(hgap=5, vgap=5\)
    self\.panel = wx\.Panel\(self\)
    sizer\.Add\(self\.panel, pos=\(0,0\), flag=wx\.EXPAND\)

sizer is later being set on the Frame instance, suggest just using a
BoxSizer and only having the Panel in it.

    box = wx\.StaticBox\(self\.panel, \-1, "Label Text Here"\)
    sizer\.Add\(box, pos=\(0,0\)\)

The StaticBox is a child of the Panel so it should not be added to
same sizer that its parent is in. The Panel should have its own sizer
that its children go into.

psizer = wx.BoxSizer()
self.panel.SetSizer(psizer)

Then later just add the bsizer to the panels sizer.

Also its a little odd but StaticBoxes are kind of a special case
widget, you should add the StaticBoxSizer that is used for laying out
the StaticBox to the layout and not the StaticBox itself (i.e remove
the above line).

    bsizer = wx\.StaticBoxSizer\(box, wx\.VERTICAL\)
    box\.SetSizer\(bsizer\)

The SetSizer call is not necessary in this case as the StaticBoxSizer
takes the StaticBox it is sizing in its constructor.

    t = wx\.StaticText\(self, \-1, "Some other text here"\)
    bsizer\.Add\(t, proportion=1, flag=wx\.ALL, border=10\)

The StaticText needs to be a child of the Panel and not the Frame
since the widget will be displayed and contained by the Panel.

    bsizer\.Fit\(box\)
    self\.SetSizer\(sizer\)
    self\.Fit\(\)

Cody

···

On Tue, Feb 9, 2010 at 1:20 PM, Patrick Tisdale <patrick.tisdale@gmail.com> wrote:

2 problems:

1. The static text should not be a child of the frame, but of the panel.

2. Massive sizer confusion. You're adding the panel to the frame's sizer, and then also adding the staticbox to the same sizer even though they are not children of the same window. And then you are assigning the staticboxsizer to the staticbox. In other words, you are incorrectly crossing all kinds of hierarchy boundaries. There are a couple simple rules for sizers that will help you avoid these kinds of mistakes:

   a. A sizer assigned to a window should only manage the children of that window, or subsizers that are managing other children of that window.

   b. StaticBox and StaticBoxSizers don't appear to follow the rules, but they really do. :wink:

Compare yours with this:

import wx

class TestFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None, -1)

         sizer = wx.BoxSizer()
         self.panel = wx.Panel(self)
         sizer.Add(self.panel, 1, wx.EXPAND)

         psizer = wx.BoxSizer(wx.VERTICAL)
         self.panel.SetSizer(psizer)

         box = wx.StaticBox(self.panel, -1, "Label Text Here")
         bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)

         t = wx.StaticText(self.panel, -1, "Some other text here")
         bsizer.Add(t, proportion=1, flag=wx.ALL, border=10)

         psizer.Add(bsizer, 1, wx.EXPAND|wx.ALL, 10)

         self.SetSizer(sizer)
         self.Fit()

app = wx.App(False)
frm = TestFrame()
frm.Show()
app.MainLoop()

···

On 2/9/10 11:20 AM, Patrick Tisdale wrote:

Hi,
I'm working on my first wxPython project, so be kind:-). I hacked up
the code snippet below from the StaticBox portion of the demo. I'm
having trouble getting the static text (t) to display properly. If I
set it's parent to self.panel, it displays on top of the StaticBox
label. If I set it's parent to self, it doesn't display at all. What
am I missing?

Thanks,
Patrick
---------------------------------------------------------------------------------------------------

class TestPanel(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None, -1)

         sizer = wx.GridBagSizer(hgap=5, vgap=5)
         self.panel = wx.Panel(self)
         sizer.Add(self.panel, pos=(0,0), flag=wx.EXPAND)

         box = wx.StaticBox(self.panel, -1, "Label Text Here")
         sizer.Add(box, pos=(0,0))

         bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
         box.SetSizer(bsizer)

         t = wx.StaticText(self, -1, "Some other text here")
         bsizer.Add(t, proportion=1, flag=wx.ALL, border=10)

         bsizer.Fit(box)
         self.SetSizer(sizer)
         self.Fit()

--
Robin Dunn
Software Craftsman

Oh, I’m definitely confused:-). Thanks for the help. I think I’ve got it straightened out now. You guys rock!

Patrick

···

On Tue, Feb 9, 2010 at 4:11 PM, Robin Dunn robin@alldunn.com wrote:

2 problems:

  1. The static text should not be a child of the frame, but of the panel.

  2. Massive sizer confusion.