wxPython: Problems with GridBagSizer
Question: I'm new to wxpython and it seems to be very powerful tool for building up GUI,
but I have a question about GridBagSizer. Could you please tell me how to
adjust the size of the items that are placed inside GridBagSizer to the size
of the frame they are supposed to be placed in. I attached the sample at the
end: the buttons from 8 to 13 go beyond the frame, but what I want is to make
the size of the buttons automatically resize to fit the frame. Any advices and
suggestions are appreciated.

Best regards, Nikita

    
    
    import wx
    
    class Frame ( wx.Frame ):
       def __init__( self ):
          wx.Frame.__init__ ( self, None, id = wx.ID_ANY, title = wx.EmptyString, pos =                wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE & ~   (wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.RESIZE_BORDER) )
          nums = (('1',(0,3)), ('2',(2,3)), ('14', (3,3)), ('15',(4,3)), ('16',(5,3)), ('17',(6,3)), ('18',(7,3)),
            ('3',(1,0)), ('4',(1,1)), ('5',(1,2)), ('6',(1,3)), ('7',(1,4)), ('8',(1,5)), ('9',(1,6)), ('10',(1,7)),
            ('11',(1,8)), ('12',(1,9)), ('13',(1,10)))
    
          panel = wx.Panel(self,-1)
          panel.SetBackgroundColour("green")
          grid = wx.GridBagSizer(2,2)
    
          for num, pos in nums:
             button = wx.Button(panel, wx.NewId(), label = num)
             grid.Add(button, pos, flag=wx.EXPAND)
    
          bs2 = wx.BoxSizer(wx.VERTICAL)
          bs2.Add(grid, 0, wx.EXPAND,0)
    
          panel.SetSizer(bs2)
          panel.Layout()
          self.mainSizer = wx.BoxSizer(wx.VERTICAL)
          self.mainSizer.Add(panel, 0, wx.EXPAND, 0)
          self.SetSizer(self.mainSizer)
          self.Layout()
    
    if __name__=='__main__':
       app=wx.PySimpleApp()
       app.frame = Frame()
       app.frame.Center()
       app.frame.Show(True)
       app.MainLoop()
    


Answer: You must allow the columns to grow by calling `AddGrowableCol`:

    
    
      for i in range(11):
          grid.AddGrowableCol(i)
    

Now, they can grow, but they won't shrink beyond their initial size! So you
should also reduce their starting size:

    
    
      for num, pos in nums:
         button = wx.Button(panel, wx.NewId(), size=(10,-1), label = num)
         #original code was:  button = wx.Button(panel, wx.NewId(), label = num)
         grid.Add(button, pos, flag=wx.EXPAND)
    

Here is the complete listing:

    
    
    import wx
    
    class Frame ( wx.Frame ):
        def __init__( self ):
           wx.Frame.__init__ ( self, None, id = wx.ID_ANY, title = wx.EmptyString, pos =
                             wx.DefaultPosition, size = wx.Size( 500,300 ),
                             style = wx.DEFAULT_FRAME_STYLE & ~   (wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.RESIZE_BORDER) )
           nums = (('1',(0,3)), ('2',(2,3)), ('14', (3,3)), ('15',(4,3)), ('16',(5,3)), ('17',(6,3)), ('18',(7,3)),
             ('3',(1,0)), ('4',(1,1)), ('5',(1,2)), ('6',(1,3)), ('7',(1,4)), ('8',(1,5)), ('9',(1,6)), ('10',(1,7)),
             ('11',(1,8)), ('12',(1,9)), ('13',(1,10)))
    
           panel = wx.Panel(self,-1)
           panel.SetBackgroundColour("green")
           grid = wx.GridBagSizer(2,2)
    
           for num, pos in nums:
               button = wx.Button(panel, wx.NewId(), size=(10,-1), label = num)
               grid.Add(button, pos, flag=wx.EXPAND)
    
           for i in range(11):
               grid.AddGrowableCol(i)
    
           bs2 = wx.BoxSizer(wx.VERTICAL)
           bs2.Add(grid, 0, wx.EXPAND,0)
    
           panel.SetSizer(bs2)
           panel.Layout()
           self.mainSizer = wx.BoxSizer(wx.VERTICAL)
           self.mainSizer.Add(panel, 0, wx.EXPAND, 0)
           self.SetSizer(self.mainSizer)
           self.Layout()
    
    if __name__=='__main__':
        app=wx.PySimpleApp()
        app.frame = Frame()
        app.frame.Center()
        app.frame.Show(True)
        app.MainLoop()
    

