layoutwxpythonsizer

How do I expand a wx.sizer of a wx.panel when the panel is expanded inside another wx.sizer?


I have a wx.Panel that has a GridBagSizer associated with it. This panel is placed inside another Sizer (BoxSizer), whose add has the expand flag - meaning that the panel takes up the full width of the sizer, which is great. ...But the panel's internal sizer does not fill the panel now.

I've tried setting the internal sizer's flag to wx.Expand|wx.ALL when I add it's components, but that didn't work. Anybody know how to make sure the sizer stays the same width as it's panel when the panel is expanded?

Edit : My code that creates the panel containing the GridBagSizer:

def getNewButton(self, bmp1, bmp2, label):
        panel = wx.Panel(self.frame, -1, pos=(0,0), style=wx.BORDER_THEME)
        sizer = wx.GridBagSizer(0, 1)

        #The button
        b = buttons.GenBitmapToggleButton(panel, wx.ID_ANY, None)
        self.frame.Bind(wx.EVT_BUTTON, self.OnToggleButton, b)
        b.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
        b.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
        mask = wx.Mask(bmp1, wx.BLUE)
        bmp1.SetMask(mask)
        b.SetBitmapLabel(bmp1)
        mask = wx.Mask(bmp2, wx.BLUE)
        bmp2.SetMask(mask)
        b.SetBitmapSelected(bmp2)
        b.SetToggle(False)
        b.SetInitialSize(size = wx.Size(30, 30))
        b.SetBezelWidth(0)

        #The Label Button
        l1 = buttons.GenButton(panel, wx.ID_ANY, label, style=wx.BORDER_NONE)
        self.frame.Bind(wx.EVT_BUTTON, self.OnFlatButton, l1)
        l1.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
        l1.Bind(wx.EVT_LEAVE_WINDOW, self.OnNonToggleMouseLeave)

        sizer.Add(b, wx.GBPosition(0,0))
        sizer.Add(l1, wx.GBPosition(0,1), flag=wx.EXPAND)

        panel.SetSizer(sizer)
        sizer.SetSizeHints(panel)
        self.buttonsList.append(ImgToggleButtonComponents(b,panel,l1))
        return panel

Solution

  • Everything you're doing here looks reasonable. I think you're missing a AddGrowableCol() call.

        sizer.Add(b, wx.GBPosition(0,0))
        sizer.Add(l1, wx.GBPosition(0,1), flag=wx.EXPAND)
        sizer.AddGrowableCol(1)
    

    I've personally had more luck with a FlexGridSizer instead of a GridBagSizer:

        fgs = wx.FlexGridSizer(gridRows[fields], 2, 0, 0)
        fgs.AddGrowableCol(1)
    
        fgs.Add(wx.StaticText(self, -1, "Contact Phone"), 0, wx.ALL, border)