Search code examples
pythonuser-interfacedatagridwxpython

how to make wxpython grid automatic fit-to-window


I am looking for a simple mode to to obtain automatic fit-to-window effect using a wx.grid.Grid object inside on of my window.

By now my 4 column grid has many white space on the right but I find this very ugly, better if all column expand or collapse based on window size change!

Is there a way to get this to works?


Solution

  • 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