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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

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

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Tim Roberts
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 :-(

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, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Martin Craig
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 :-(

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, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="zmDe6NZxBQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ti...@...
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Anders Munch
In reply to this post by Martin Craig
Martin Craig wrote:
> 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?

You are using the ClientSize of the grid itself to size the grid, which seems kinda recursive. Try using the ClientSize of what it's contained in; replace
        w, h = self.GetClientSize()
with
        w,h = self.GetParent().GetClientSize()
It won't be pixel-perfect, because it doesn't account for the grid chrome. You may need to fudge a little.

regards, Anders

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...