Hi, I’m struggling with a wx.Grid - I want it to expand and shrink as the window is resized, and I also want to resize the columns dynamically so the required number are always visible.
Minimal example code is below - if you expand the window, the columns grow so the list fits. But if you shrink the window, the list stays the same size and runs off the edge
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 size initially, will expand on resize
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
print(w, cw)
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=“Input”), (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()