Has anyone actually tried out wx.WrapSizer.IsSpaceItem? It is now overridable in Phoenix, but it just doesn’t work for me…
class MyWrapSizer(wx.WrapSizer):
def IsSpaceItem(self, item):
window = item.GetWindow()
if isinstance(window, wx.StaticText):
return True # if sizer item has a static text inside, then pretend it’s a space
return wx.WrapSizer.IsSpaceItem(self, item)
class MainFrame(wx.Frame):
def init(self, *a, **k):
wx.Frame.init(self, *a, **k)
p = wx.Panel(self)
sizer = MyWrapSizer(wx.HORIZONTAL, wx.REMOVE_LEADING_SPACES)
for _ in range(5):
sizer.Add(wx.Button(p, size=(50, 50)))
sizer.Add(wx.StaticText(p, -1, ‘Hello!’)) # now this one should be treated as space
for _ in range(5):
sizer.Add(wx.Button(p, size=(50, 50)))
p.SetSizer(sizer)
``
This should be a reasonable thing to ask - yet if fails with
wx._core.wxAssertionError: C++ assertion “!sizer || m_containingSizer != sizer” failed at …\src\common\wincmn.cpp(2470) in wxWindowBase::SetContainingSizer(): Adding a window to the same sizer twice?
The above exception was the direct cause of the following exception:
SystemError: <class ‘wx._core.SizerItem’> returned a result with an error set
``
Please note that re-creating the original behaviour works fine:
class MyWrapSizer(wx.WrapSizer):
def IsSpaceItem(self, item):
return item.IsSpacer() # this works like a charm
``
However, testing for SizerItem.IsSizer (instead of IsSpacer) behaves erratically depending of the actual content of the (sub)sizer:
class MyWrapSizer(wx.WrapSizer):
def IsSpaceItem(self, item):
return item.IsSizer()
class MainFrame(wx.Frame):
def init(self, *a, **k):
wx.Frame.init(self, *a, **k)
p = wx.Panel(self)
sizer = MyWrapSizer(wx.HORIZONTAL, wx.REMOVE_LEADING_SPACES)
for _ in range(5):
sizer.Add(wx.Button(p, size=(50, 50)))
s = wx.BoxSizer()
s.Add(wx.TextCtrl(p, -1, ‘Hello!’))
sizer.Add(s) # this should be a space, now
for _ in range(5):
sizer.Add(wx.Button(p, size=(50, 50)))
p.SetSizer(sizer)
``
This doesn’t fail, but it doesn’t quite work either. If you put a StaticText inside the sub-sizer instead, it looks better, but still can’t wrap it nicely.
My guess is, even if wx.WrapSizer.IsSpaceItem is indeed overridable, the behind-the-scenes machinery of wx.Sizer.Add is just too much for wxPython to tamper with.
Or - it’s just me missing something obvious here?
riccardo