wx.Grid expands away from its allocated sizer

954 views
Skip to first unread message

Alexandre Almosni

unread,
Jun 28, 2019, 10:03:31 AM6/28/19
to wxPython-users
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()

Robin Dunn

unread,
Jul 3, 2019, 4:21:12 PM7/3/19
to wxPython-users
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

Alexandre Almosni

unread,
Jul 10, 2019, 5:53:09 AM7/10/19
to wxPython-users
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.
Reply all
Reply to author
Forward
0 new messages