Baffled by sizers.

I've been using wx Python and sizers for years, and sizers
still baffle me.

They only do what I expect about 30% of the time, and I usually
end up trying random changes for several hours and then giving
up on the last 20% of what I'm trying to accomplish.

Is there any good documentation on wx sizers?

I've found the stuff on the wxpython wiki, but all that has is
a list of what sorts of sizers there are. There are no
examples and no usage information. The wxWidgets documentation
pages aren't helpful. The descriptions of what the flags/styles
do are so vaugue you might as well just skip the descriptions
and try them to see what happens.

Here's short example:

     1 #!/usr/bin/python
     2 import wx
     3
     4 class MyFrame(wx.Frame):
     5 def __init__(self, parent, title):
     6 wx.Frame.__init__(self, parent, -1, title)
     7 panel = wx.Panel(self)
     8 vsizer = wx.BoxSizer(wx.VERTICAL)
     9
    10 hsizer = wx.BoxSizer(wx.HORIZONTAL)
    11
    12 hsizer.Add(wx.StaticText(panel,-1,'Left'))
    13
    14 hs = wx.BoxSizer(wx.HORIZONTAL)
    15 hs.Add(wx.StaticText(panel,-1,'CenterL'))
    16 hs.Add(wx.StaticText(panel,-1,'CenterR'),flag=wx.LEFT,border=10)
    17 hsizer.Add(hs,1,flag=wx.ALIGN_CENTER)
    18
    19 hsizer.Add(wx.StaticText(panel,-1,'Right'))
    20
    21 vsizer.Add(hsizer,1,flag=wx.ALL+wx.EXPAND,border=10)
    22
    23 vsizer.Add(wx.StaticText(panel,-1,'Centered',style=wx.ALIGN_CENTER),1,flag=wx.EXPAND)
    24 panel.SetSizerAndFit(vsizer)
    25 panel.Layout()
    26 vsizer.Fit(self)
    27
    28 class MyApp(wx.App):
    29 def OnInit(self):
    30 frame = MyFrame(None, "Baffled by Sizers")
    31 frame.Show(True)
    32 self.SetTopWindow(frame)
    33 return True
    34
    35 app = MyApp(False)
    36 app.MainLoop()

What I expect is something like this:

         Left CenterL CenterR Right

                           Centered

What I get after resizign the window is

         Left Right

             CenterL CenterR

                           Centered
  
Question 1: Why is the hbox containing "CenterL CenterR" not
            centered? Doesn't line 17 says to center it? How
            do I get the (CenterL CenterR) centered?

Question 2: Why are the three things in the first hbox (Left,
            (CenterL CenterR), Right) not aligned vertically?

All I'm trying to do is create a horizontal sizer containing
three things: one left justified, one centered, one right
justified. I've spend hours messing with sizers, andthere just
doesn't seem to be any way to do it.

···

--
Grant Edwards grante Yow! Let me do my TRIBUTE
                                  at to FISHNET STOCKINGS...
                               visi.com

I gave up on using a horizontal box sizer (thought it would
seem to be the obvious choice for controlling the horizontal
position of a set of objects). Using a flexGridSizer with a
single row I can produce a close enough approximation of what I
want.

···

On 2005-09-02, Grant Edwards <grante@visi.com> wrote:

All I'm trying to do is create a horizontal sizer containing
three things: one left justified, one centered, one right
justified. I've spend hours messing with sizers, and there just
doesn't seem to be any way to do it.

--
Grant Edwards grante Yow! Do you need
                                  at any MOUTH-TO-MOUTH
                               visi.com resuscitation?

try changing what you want to something like this:

Left - 1/2 remainer of space - CenterLCenterR - 1/2 remaniner of space - Right
1/2 remainer of space - Centered - 1/2 remaniner of space

line1.Add(left)
line1.Add((-1,-1), 1)
line1.Add(centerL)
line1.Add(centerR)
line1.Add((-1,-1), 1)
line1.Add(right)

line2.Add((-1,-1), 1)
line2.Add(centered)
line2.Add((-1,-1), 1)

vbox.Add(line1, 0, wx.EXPAND)
vbox.Add(line2, 0, wx.EXPAND)

always remebere that beside controls you have to manage the space too, so make the space an element.

Peter.

···

On Fri, 02 Sep 2005 16:54:35 +0300, Grant Edwards <grante@visi.com> wrote:

What I expect is something like this:

         Left CenterL CenterR Right

                           Centered

Ah ah! Thanks!

Somehow I missed out on the fact that you could use spaces like
TeX "glue".

Though it still doesn't guarantee that the "center" stuff is
centered, it's close enough for now.

···

On 2005-09-02, Peter Damoc <pdamoc@gmx.net> wrote:

On Fri, 02 Sep 2005 16:54:35 +0300, Grant Edwards <grante@visi.com> wrote:

What I expect is something like this:

         Left CenterL CenterR Right

                           Centered

try changing what you want to something like this:

Left - 1/2 remainer of space - CenterLCenterR - 1/2 remaniner of space - Right
1/2 remainer of space - Centered - 1/2 remaniner of space

line1.Add(left)
line1.Add((-1,-1), 1)
line1.Add(centerL)
line1.Add(centerR)
line1.Add((-1,-1), 1)
line1.Add(right)

line2.Add((-1,-1), 1)
line2.Add(centered)
line2.Add((-1,-1), 1)

vbox.Add(line1, 0, wx.EXPAND)
vbox.Add(line2, 0, wx.EXPAND)

always remebere that beside controls you have to manage the space too, so
make the space an element.

--
Grant Edwards grante Yow! Were these parsnips
                                  at CORRECTLY MARINATED in
                               visi.com TACO SAUCE?

the world could end tomorrow so... that will have to do :wink:

Peter.

···

On Fri, 02 Sep 2005 17:36:16 +0300, Grant Edwards <grante@visi.com> wrote:

Ah ah! Thanks!

Somehow I missed out on the fact that you could use spaces like
TeX "glue".

Though it still doesn't guarantee that the "center" stuff is
centered, it's close enough for now.