wx.Grid expands away from its allocated sizer

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

wx.Grid expands away from its allocated sizer

Alexandre Almosni
A minimal example is below. I have a frame with two panels left and right that I'd like of equal width. The right panel has a Grid. Now if that grid has too many cells, it will expand and take over the left panel. If there are items on the left panel (say buttons) it will not take them over, but it will effectively shrink the left panel as much as possible.

How can I ensure my grid on the right hand side doesn't take more than half the frame? I just want scrollbars if there are two many cells.

Thanks a lot!

import wx
from wx.grid import Grid


class TestWindow(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GridTest", size=(800, 600))

sizer = wx.BoxSizer(wx.HORIZONTAL)

panel_one = wx.Panel(self)
panel_two = wx.Panel(self)

panel_two_sizer = wx.BoxSizer(wx.VERTICAL)
grid = Grid(panel_two)
grid.CreateGrid(2, 20) # it would work fine with a 2x2 grid
panel_two_sizer.Add(grid, 1, wx.EXPAND)
panel_two.SetSizer(panel_two_sizer)

sizer.Add(panel_one, proportion=1, wx.EXPAND)
sizer.Add(panel_two, proportion=1, wx.EXPAND)

self.SetSizer(sizer)

pass


if __name__ == "__main__":
app = wx.App()
frame = TestWindow().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].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/a42f30ca-55bd-425f-83d1-9e2803ad0c3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: wx.Grid expands away from its allocated sizer

Robin Dunn
On Friday, June 28, 2019 at 7:03:31 AM UTC-7, Alexandre Almosni wrote:
A minimal example is below. I have a frame with two panels left and right that I'd like of equal width. The right panel has a Grid. Now if that grid has too many cells, it will expand and take over the left panel. If there are items on the left panel (say buttons) it will not take them over, but it will effectively shrink the left panel as much as possible.

The proportion parameter on box sizers determines how the left-over space is divided up between the items being managed by the sizer. In other words, items will first get the space they require determined by their BestSize or MinSize, if possible, and then anything remaining is divided between the items based on the proportions.

In your test case the left panel has a BestSize of (1,1) because there is nothing on the panel. The panel on the right has a BestSize of (1682, 70), because of its sizer and the grid it contains. (You can easily this and much more layout-related info using the Widget Inspection Tool. https://wiki.wxpython.org/Widget_Inspection_Tool) So, since the left panel is telling the sizer that it only needs one pixel, and the right panel wants more than is available in the frame, then the sizer gives everything minus one to the right panel, and since there is nothing left over then nothing is divided up proportionally.

Since the MinSIze takes precedence with the sizers then one way to make your test case work the way you want is to set the MinSize of both panels to (1,1). That way they both will get 1 pixel width based on their MinSize, and then everything else will be divided between the 2 panels using the sizer item proportion values.

--
Robin


 

How can I ensure my grid on the right hand side doesn't take more than half the frame? I just want scrollbars if there are two many cells.

Thanks a lot!

import wx
from wx.grid import Grid


class TestWindow(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GridTest", size=(800, 600))

sizer = wx.BoxSizer(wx.HORIZONTAL)

panel_one = wx.Panel(self)
panel_two = wx.Panel(self)

panel_two_sizer = wx.BoxSizer(wx.VERTICAL)
grid = Grid(panel_two)
grid.CreateGrid(2, 20) # it would work fine with a 2x2 grid
panel_two_sizer.Add(grid, 1, wx.EXPAND)
panel_two.SetSizer(panel_two_sizer)

sizer.Add(panel_one, proportion=1, wx.EXPAND)
sizer.Add(panel_two, proportion=1, wx.EXPAND)

self.SetSizer(sizer)

pass


if __name__ == "__main__":
app = wx.App()
frame = TestWindow().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].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/2a2b186d-cf7b-4ffa-b3bf-e4625bdfae6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: wx.Grid expands away from its allocated sizer

Alexandre Almosni
Robin, thank you so much for the detailed reply and the link to the widget inspection tool which will no doubt save me a lot of time! Thanks again for the great work.

On Wednesday, July 3, 2019 at 9:21:12 PM UTC+1, Robin Dunn wrote:
On Friday, June 28, 2019 at 7:03:31 AM UTC-7, Alexandre Almosni wrote:
A minimal example is below. I have a frame with two panels left and right that I'd like of equal width. The right panel has a Grid. Now if that grid has too many cells, it will expand and take over the left panel. If there are items on the left panel (say buttons) it will not take them over, but it will effectively shrink the left panel as much as possible.

The proportion parameter on box sizers determines how the left-over space is divided up between the items being managed by the sizer. In other words, items will first get the space they require determined by their BestSize or MinSize, if possible, and then anything remaining is divided between the items based on the proportions.

In your test case the left panel has a BestSize of (1,1) because there is nothing on the panel. The panel on the right has a BestSize of (1682, 70), because of its sizer and the grid it contains. (You can easily this and much more layout-related info using the Widget Inspection Tool. <a href="https://wiki.wxpython.org/Widget_Inspection_Tool" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwiki.wxpython.org%2FWidget_Inspection_Tool\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEhrO8KxaILhtRYxoTuciRjADcbdg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwiki.wxpython.org%2FWidget_Inspection_Tool\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEhrO8KxaILhtRYxoTuciRjADcbdg&#39;;return true;">https://wiki.wxpython.org/Widget_Inspection_Tool) So, since the left panel is telling the sizer that it only needs one pixel, and the right panel wants more than is available in the frame, then the sizer gives everything minus one to the right panel, and since there is nothing left over then nothing is divided up proportionally.

Since the MinSIze takes precedence with the sizers then one way to make your test case work the way you want is to set the MinSize of both panels to (1,1). That way they both will get 1 pixel width based on their MinSize, and then everything else will be divided between the 2 panels using the sizer item proportion values.

--
Robin


 

How can I ensure my grid on the right hand side doesn't take more than half the frame? I just want scrollbars if there are two many cells.

Thanks a lot!

import wx
from wx.grid import Grid


class TestWindow(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GridTest", size=(800, 600))

sizer = wx.BoxSizer(wx.HORIZONTAL)

panel_one = wx.Panel(self)
panel_two = wx.Panel(self)

panel_two_sizer = wx.BoxSizer(wx.VERTICAL)
grid = Grid(panel_two)
grid.CreateGrid(2, 20) # it would work fine with a 2x2 grid
panel_two_sizer.Add(grid, 1, wx.EXPAND)
panel_two.SetSizer(panel_two_sizer)

sizer.Add(panel_one, proportion=1, wx.EXPAND)
sizer.Add(panel_two, proportion=1, wx.EXPAND)

self.SetSizer(sizer)

pass


if __name__ == "__main__":
app = wx.App()
frame = TestWindow().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].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/b13715bd-6357-4e04-9c27-9709081046d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.