I’m trying to make a Panel act like a button, following the example of wx.lib.buttons.GenButton. (I know this is clumsy, but the panels are potentially complex and I’m using sizers for layout.) This works for the panel itself, but clicking on any of the controls contained in the panel - just StaticText and StaticBitmap objects - does not propagate the event. I assume this is because the events I’m binding aren’t wx.CommandEvents, but I can’t intercept them directly either: I tried binding to the appropriate events with a text control as the source, but that doesn’t work. How can I get the click to “pass through” to the panel? Or is what I’m doing futile?
thanks,
Nat
sample code:
mport wx
class MyFrame (wx.Frame) :
def init (self) :
wx.Frame.init(self, None, -1, “Test frame”, style=wx.DEFAULT_FRAME_STYLE)
panel = MyPanel(self)
szr = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(szr)
#self.Fit()
self.Layout()
class MyPanel (wx.Panel) :
def init (self, parent) :
wx.Panel.init(self, parent, -1)
self.is_active = False
self.SetBackgroundColour((220,220,220))
szr = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(szr)
txt = wx.StaticText(self, -1, “This is some text. Click me!”)
szr.Add(txt, 0, wx.ALL|wx.EXPAND, 10)
szr.Layout()
szr.Fit(self)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown, txt)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp, txt)
self.Bind(wx.EVT_MOTION, self.OnMotion, txt)
def OnLeftDown (self, event) :
self.CaptureMouse()
self.SetFocus()
self.set_active()
def OnLeftUp (self, event) :
if self.is_active :
if self.HasCapture() :
self.ReleaseMouse()
self.reset_active()
print “panel clicked”
def set_active (self) :
self.is_active = True
self.SetBackgroundColour((130,160,180))
self.Refresh()
def reset_active (self) :
self.SetBackgroundColour((220,220,220))
self.Refresh()
self.is_active = False
def OnMotion(self, event):
if not self.IsEnabled() or not self.HasCapture():
return
if event.LeftIsDown() and self.HasCapture():
x,y = event.GetPositionTuple()
w,h = self.GetClientSizeTuple()
if self.is_active and (x<0 or y<0 or x>=w or y>=h):
self.reset_active()
return
event.Skip()
if name == “main” :
a = wx.App(0)
frame = MyFrame()
frame.Show()
a.MainLoop()