For more hours than I'd like to admit I've trying to put a custom
control (with a wx.EVT_PAINT handler) in a sizer.
I eventually managed a very simple example, but was surprised that the
only difference between the example that worked and the many that
didn't was the type of sizer in which I placed the control:
wx.gridsizer = working, BoxSizers/Flexgridsizers=not working (see
code below). On further investigation it seems that when using
BoxSizers or Flexgridsizers an EVT_PAINT event is never raised, since
the bound event handler never runs. Can anyone explain what's going
on?
import wx
class Canvas(wx.PyControl):
def __init__(self, parent):
super(Canvas, self).__init__(parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
print "OnPaint called as expected"
dc = wx.PaintDC(self)
dc.DrawText("Hello World!", 5, 5)
class MyApp(wx.App):
def OnInit(self):
self.frame = Example(None, title="Top frame")
self.frame.Show()
return True
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.panel = MyPanel(self)
frameSizer = wx.BoxSizer(wx.VERTICAL)
frameSizer.Add(self.panel, 1, wx.EXPAND)
self.SetSizer(frameSizer)
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
self.__DoLayout()
def __DoLayout(self):
wdg = Canvas(self)
# if msizer is a BoxSizer nothing is visible, and OnPaint is never
called.
# if msizer is a GridSizer it works as expected
#msizer = wx.BoxSizer(wx.VERTICAL)
msizer = wx.GridSizer(1,1,0,0)
msizer.AddSpacer((20,20))
msizer.Add(wdg, 0, wx.EXPAND, 5)
msizer.AddSpacer((20,20))
self.SetSizer(msizer)
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()