Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 768
tidy Avatar asked Jul 16 '14 06:07

tidy


1 Answers

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.

like image 200
user2963623 Avatar answered Oct 11 '22 07:10

user2963623