[wxPython-users] Problem with GridBagSizer and span

Werner F. Bruhin werner.bruhin at free.fr
Mon Jul 23 08:35:40 PDT 2007


Hi Frank,

Frank Millman wrote:
> Hi all
>
> I understand what the 'span' argument to wx.GridBagSizer.Add() means, but=
 it
> is not behaving as I would expect.
>
> Assume I have two columns in my GBS, one with a width of 20 and one with a
> width of 30.
>
> If I add a row containing an element with a width of 40, spanning both
> columns, I expected that the existing sizes and positions would not chang=
e,
> as there is ample room to fit the new row into the available width.
>
> In practice, the overall width is increased, and ugly spaces start to
> appear.
>
> Run the attached sample to see what I mean. You will see that the problem
> gets worse as the width of the new element increases, even though it would
> fit into the original available width.
>
> Is this expected behaviour? Is there a way to achieve what I want?
>   =

I think so, one has to trick it into doing what one wants, at least that =

is my experience.

Attached a modified version, which is better (I think) but still not =

perfect.

You might also want to read this:
http://wiki.wxpython.org/wxGridBagSizer

I did a lot of trial and error on this, that is why I added the =

inspection stuff to it (when you run press ctrl-alt-i).

What would help so is to make the sizer "visible", and I am pretty sure =

there is a way to do this but can not find it at the moment.

Hopefully some others will have some other tips on working with this and =

it is giving me lots of gray hairs when I use gbs's.
Werner
-------------- next part --------------
#!/usr/bin/env python
import wx

#----------------------------------------------------------------------
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"")
        subframe1 =3D MySubFrame(self,lng=3D0)
        subframe1.SetPosition((250,80))
        subframe1.Show(True)
        subframe2 =3D MySubFrame(self,lng=3D1)
        subframe2.SetPosition((250,280))
        subframe2.Show(True)
        subframe3 =3D MySubFrame(self,lng=3D2)
        subframe3.SetPosition((250,520))
        subframe3.Show(True)

class MySubFrame(wx.Frame):
    def __init__(self,parent,lng):
        wx.Frame.__init__(self,parent,-1,"GridBagSizer : lng=3D%s"%lng)
        panel =3D MyPanel(self,lng)
        self.ClientSize =3D panel.BestSize
        self.parent =3D parent
        self.Bind(wx.EVT_CLOSE,self.onClose)

    def onClose(self,evt):
        self.parent.Close()

class MyPanel(wx.Panel):
    def __init__(self,frame,lng):
        wx.Panel.__init__(self,frame,-1)
        =

        self.gbs =3D wx.GridBagSizer(8,8)
        self.gbs.SetEmptyCellSize((0,0))

        # trick the sizers min cell size
        self.gbs.Add((10,0),(0,0), (1, 5))
        =

        trickSpan =3D 4

        row =3D 0

        row +=3D 1
        self.gbs.Add(wx.StaticText(self,-1,'Type:'),(row,0))
        self.gbs.Add(wx.TextCtrl(self,-1,'House',size=3D(100,-1)),(row,1), =
(1, trickSpan))

        if lng:
            row +=3D 1
            self.gbs.Add(wx.StaticText(self,-1,'This is a very long string =
'*lng),
                (row,0),(1,trickSpan+1))

        row +=3D 1
        self.gbs.Add(wx.StaticText(self,-1,'Address:'),(row,0))
        self.gbs.Add(wx.TextCtrl(self,-1,'123 Main Road',size=3D(300,-1)),(=
row,1), (1, trickSpan))

        row +=3D 1
        self.gbs.Add(wx.StaticText(self,-1,'Suburb:'),(row,0))
        self.gbs.Add(wx.TextCtrl(self,-1,'Forest Town',size=3D(300,-1)),(ro=
w,1), (1, trickSpan))

        row +=3D 1
        self.gbs.Add(wx.StaticText(self,-1,'City:'),(row,0))
        self.gbs.Add(wx.TextCtrl(self,-1,'Atlantic City',size=3D(200,-1)),(=
row,1), (1, trickSpan))

##        # add some space to the right and bottom
##        row +=3D 1
##        self.gbs.Add((5,5),(row,3))
        # put 5 pixels around it all
        pageBoxSizer =3D wx.BoxSizer()
        pageBoxSizer.Add(self.gbs, 0, wx.TOP | wx.BOTTOM | wx.LEFT, 5)

        self.SetSizerAndFit(pageBoxSizer)

from wx.lib.mixins.inspection import InspectionMixin
class MyApp(wx.App, InspectionMixin):     =

    def OnInit(self):
        InspectionMixin.Init(self)
        frame =3D MyFrame()
        frame.Show(False)
        self.SetTopWindow(frame)
        return True

app =3D MyApp(0)     # Create an instance of the application class
app.MainLoop()     # Tell it to start processing events



More information about the wxpython-users mailing list