Making a grid resize to fit in window

15 views
Skip to first unread message

Martin Craig

unread,
Mar 20, 2017, 11:41:53 AM3/20/17
to wxPython-users
Hi, new to wxPython, and getting very frustrated trying to make a wx.Grid resize correctly!

It's a simple horizontal list, and I want a fixed number of colums to resize to fit the space available. It's working fine when the window is expanded, but when the window is shrunk the grid does not shrink - it just goes off the edge of the window!

Here is my minimal code - any help much appreciated.

Martin

import wx
import wx.grid

class NumberList(wx.grid.Grid):
    def __init__(self, parent):
        super(NumberList, self).__init__(parent, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.n=10
        self.CreateGrid(1, self.n)
        self.SetRowLabelSize(0)
        self.SetColLabelSize(0)
        self.Bind(wx.EVT_SIZE, self.on_size)
        # Set small initial size for columns - will increase when we get resize event
        for i in range(self.n):
            self.SetColSize(i, 20)

    def on_size(self, event):
        event.Skip()
        w, h = self.GetClientSize()
        cw = w / self.n
        for i in range(self.n):
            self.SetColSize(i, cw)
        self.Refresh()

class Gui(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="test", size=(800, 400))

        sizer = wx.GridBagSizer(vgap=5, hgap=10)
        sizer.Add(wx.StaticText(self, label="Data"), (0, 0))
        sizer.Add(NumberList(self), pos=(0, 1), span=(1,2), flag=wx.EXPAND)

        sizer.AddGrowableCol(2, 1)
        self.SetSizer(sizer)
        self.Layout()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    top = Gui()
    top.Show()
    app.MainLoop()

Reply all
Reply to author
Forward
0 new messages