Search code examples
wxpython

How to correclty size a wx.grid.Grid so it doesn't cover other controls?


I'm try to correctly use a wx.grid inside a sizer, but the grid is escaping is portion of the sizer. I want the wx.grid to stay in its portion of the grid. I'm expecting that I would end up w/ scrollbars if needed.

I have two controls in a BoxSizer, one is a wx.grid.Grid, and the other is a wx.TextCtrl. The grid has more rows than will fix in the available space.

The problem is that the grid covers the textctl rather than stopping at top of the textctl. Scrollbars do show up if the grid is bigger than the window size


import wx
import wx.grid

app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Grid Test", size=(1000, 1200))
panel  = wx.Panel(frame, wx.ID_ANY, size=(-1, -1))

mygrid = wx.grid.Grid(panel, size=(-1, -1))

num_rows = 50
mygrid.CreateGrid(num_rows, 10)

for i in range(num_rows):
    mygrid.SetCellValue(i, 0, "Yes")
    mygrid.SetCellValue(i, 1, "This is a DUT")
    mygrid.SetCellValue(i, 2, "TP-Link TL-WR841N")
    mygrid.SetCellValue(i, 3, "802.11ax (2.4 GHz)")
    mygrid.SetCellValue(i, 4, "WPA2-PSK with AES")
    mygrid.SetCellValue(i, 5, "11 - 2412 Mhz")
    mygrid.SetCellValue(i, 6, "160 MHz")
    mygrid.SetCellValue(i, 7, "Fail - Iperf")
    mygrid.SetCellValue(i, 8, "%daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" % i)
    mygrid.SetCellValue(i, 9, "%dbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" % i)

mygrid.SetRowLabelSize(0)
mygrid.AutoSizeColumns(True)
mygrid.EnableDragRowSize(False)
mygrid.AutoSize()

tc = wx.TextCtrl(panel, size=(-1, -1), style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH2)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.AddSpacer(10)
sizer.Add(mygrid, 2, wx.EXPAND | wx.LEFT | wx.RIGHT, 50)
sizer.AddSpacer(20)
sizer.Add(tc, 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 50)
sizer.AddSpacer(10)

sizer.SetSizeHints(panel)
panel.SetSizerAndFit(sizer)

frame.Show()
app.MainLoop()

This is running on:

Ubuntu 18.04.2

libwxbase3.0-0v5 (3.0.4+dfsg-3)
libwxgtk3.0-gtk3-0v5 (3.0.4+dfsg-3)
python-wxgtk3.0 (3.0.2.0+dfsg-7)
python-wxversion (3.0.2.0+dfsg-7)

libgtk-3-0 (3.22.30-1ubuntu2)

Solution

  • Just give the grid a size.
    e.g.

    mygrid = wx.grid.Grid(panel, size=(1000, 600))
    

    enter image description here

    Edit:

    It is my belief that unless the grid has a size or a MinSize, the sizer gets confused.

    So either give it a size (as above)

    or

    give it a Minsize i.e.

    mygrid.SetMinSize((400,600))