I know we’ve been here before but I had hoped this would have been fixed with a newer release of wxPython. I’ve replaced my original example and replaced it with something much simpler. You had said that you usually avoid using borders with gridsizers, but shouldn’t they still work as documented? Is this a problem with wxPython, or with the underlying wxWidgets?
The following example is taken almost verbatim from wxPython in Action. It demonstrates that the GrizSizer border parameter does not work as described in the documentation. I am using the current versions of both wxPython and Python.
"""
Name:
GridSizer.py
Description:
Demo of possible problem with grid sizer border parameter. What I expect
to see based on my parameters is a 3x3 grid of BlockWindow objects with
an outside border of size 10. Instead I get a 3x3 grid with a gap between
columns 2 & 3, and between rows 2 & 3 (1-relative). The code from this
example is taken almost verbatim from wxPython in Action. I have made
minor changes to remove a redundant list (labels), to improve readability,
and to correct incompatibilities with Python 3.8x. Note that using buttons
instead of the BlockWindow from the book example produces the same result.
"""
import wx
class BlockWindow(wx.Panel):
def __init__(self, parent, ID=-1, label="", pos=wx.DefaultPosition, size=(100, 25)):
wx.Panel.__init__(self, parent, ID, pos, size, wx.RAISED_BORDER, label)
self.label = label
self.SetBackgroundColour("white")
self.SetMinSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
sz = self.GetClientSize()
dc = wx.PaintDC(self)
w,h = dc.GetTextExtent(self.label)
dc.SetFont(self.GetFont())
dc.DrawText(self.label, (sz.width-w)//2, (sz.height-h)//2)
B,T,L,R = wx.BOTTOM, wx.TOP, wx.LEFT, wx.RIGHT
flags = {"one": T | L,
"two": T,
"three": T | R,
"four": L,
"five": 0,
"six": R,
"seven": B | L,
"eight": B,
"nine": B | R}
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Sizer Problem Demo")
sizer = wx.GridSizer(rows=3, cols=3, hgap=0, vgap=0)
for label,flag in flags.items():
bw = BlockWindow(self, label=label)
#bw = wx.Button(self, label=label) #note - same problem with Buttons
sizer.Add(bw, 0, flag, 10)
self.SetSizer(sizer)
self.Fit()
app = wx.App()
TestFrame().Show()
app.MainLoop()