how to make wxpython grid automatic fit-to-window

You need to catch the window resizing event and bind it to an event handler. I have shown it in this example:

import wx.grid

class Frame ( wx.Frame ):

        def __init__( self, parent ):
                wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 650,480 ), style = wx.DEFAULT_FRAME_STYLE|wx.RESIZE_BORDER|wx.TAB_TRAVERSAL )

                self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
                sizer = wx.BoxSizer( wx.VERTICAL )
                self.panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
                self.inner_sizer = wx.BoxSizer( wx.VERTICAL )
                self.grid = wx.grid.Grid( self.panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )

                # Grid
                self.grid.CreateGrid( 10, 4 )
                self.grid.EnableEditing( True )
                self.grid.EnableGridLines( True )
                self.grid.EnableDragGridSize( False )
                self.grid.SetMargins( 0, 0 )

                # Columns
                self.grid.EnableDragColMove( False )
                self.grid.EnableDragColSize( True )
                self.grid.SetColLabelSize( 30 )
                self.grid.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

                # Rows
                self.grid.EnableDragRowSize( True )
                self.grid.SetRowLabelSize( 80 )
                self.grid.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

                # Label Appearance

                # Cell Defaults
                self.grid.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
                self.inner_sizer.Add( self.grid, 1, wx.ALL|wx.EXPAND, 5 )


                self.panel.SetSizer( self.inner_sizer )
                self.panel.Layout()
                self.inner_sizer.Fit( self.panel )
                sizer.Add( self.panel, 1, wx.EXPAND |wx.ALL, 0 )

                self.grid.Bind(wx.EVT_SIZE, self.OnSize)

                self.SetSizer( sizer )
                self.Layout()
                self.Centre( wx.BOTH )
                self.Show()

        def OnSize(self, event):
                width, height = self.GetClientSizeTuple()
                for col in range(4):
                        self.grid.SetColSize(col, width/(4 + 1))


if __name__ == "__main__":
        app = wx.App()
        Frame(None)
        app.MainLoop()

If you have a variable number of columns simply put that variable in place of 4 in OnSize.


Use wx.EXPAND|wx.ALL function to expand your grid

myGrid = gridlib.Grid(panel)
sizer.Add(myGrid, 1, wx.EXPAND|wx.ALL)

Edit - In my experience, I had to add the sizer to the parent window, and also add the .Fit, or it wouldn't size until it was dragged.

myGrid = gridlib.Grid(mypanel)
BS = wx.BoxSizer()
BS.AddWindow(myGrid, 1, flag=wx.EXPAND)
mypanel.SetSizer(BS)    # this was important in my code
BS.Fit(mypanel)     # this may only be necessary to force first fit