wx.Grid will expand, but won't shrink!

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 :frowning:

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()

Martin Craig wrote:

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 :frowning:

The trouble, I think, is related to the GridBagSizer, where the grid
spans two columns, one of which is growable. If I use a box sizer
instead, your code works as expected:

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(wx.StaticText(self, label="Input"), 0)
        sizer.Add(NumberList(self), 1, flag=wx.EXPAND)
        self.SetSizer(sizer)
        self.Layout()

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hi, thanks for the reply,

It does seem to be related to the GridBagSizer, however it still occurs when the list does

not span multiple columns, e.g.

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), flag=wx.EXPAND)

sizer.AddGrowableCol(1, 1)

self.SetSizer(sizer)

self.Layout()

I can make it work with a FlexGridSizer, but unfortunately I need multi-column spanning in the real application.

Seems like a bug to me - any thoughts?

Martin.

···

On Monday, March 20, 2017 at 6:49:55 PM UTC, Tim Roberts wrote:

Martin Craig wrote:

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 :frowning:

The trouble, I think, is related to the GridBagSizer, where the grid

spans two columns, one of which is growable. If I use a box sizer

instead, your code works as expected:

    sizer = wx.BoxSizer(wx.HORIZONTAL)

    sizer.Add(wx.StaticText(self, label="Input"), 0)

    sizer.Add(NumberList(self), 1, flag=wx.EXPAND)

    self.SetSizer(sizer)

    self.Layout()


Tim Roberts, ti...@probo.com

Providenza & Boekelheide, Inc.