how to detect mouse click on a panel?

1,519 views
Skip to first unread message

wrybread

unread,
Jan 6, 2010, 7:22:08 PM1/6/10
to wxPython-users
I need to detect when a user has clicked a panel, as opposed to a
specific object in that panel. I'm wondering if anyone can think of
any clever way to do this?

I'm using Windows if that matters.

Robin Dunn

unread,
Jan 6, 2010, 8:17:24 PM1/6/10
to wxpytho...@googlegroups.com
On 1/6/10 4:22 PM, wrybread wrote:
> I need to detect when a user has clicked a panel, as opposed to a
> specific object in that panel. I'm wondering if anyone can think of
> any clever way to do this?

Bind event handlers for the mouse events to the panel.

--
Robin Dunn
Software Craftsman
http://wxPython.org

wrybread

unread,
Jan 6, 2010, 8:27:48 PM1/6/10
to wxPython-users
I tried that using:

self.panel.Bind(wx.EVT_LEFT_DOWN, self.onMouseClick)

But couldn't get any events.

I am able to get some reaction from:

self.panel.Bind(wx.EVT_MOUSE_EVENTS, self.onMouseClick)

But the only events I'm getting from that is when the mouse first
enters the window, and first leaves.

Is there some other method of binding I should be using?

joelb

unread,
Jan 7, 2010, 6:09:35 PM1/7/10
to wxPython-users
I tried a couple tests and seem to be getting the EVT_LEFT_DOWN from
my panel(s). I also noticed that the first handler to capture the
event is the only one fired and the event is NOT passed down. My
guess is that your event is being caught somewhere higher up in the
app.

Here's my test:

"""test_panel.py - Testing wx.EVT_LEFT_DOWN event in a panel"""
import wx

class TestFrame(wx.Frame):
def __init__(self, parent, id, **kwargs):
""" Simple test frame"""
wx.Frame.__init__(self, parent, id, **kwargs)
self.panel = TestPanel(self, -1)
# Comment the following line to see the event handler from
TestPanel
self.panel.Bind(wx.EVT_LEFT_DOWN, self.onMouseClick)
self.Centre()
self.Show(True)

def onMouseClick(self, event):
print "Defined in TestFrame"


class TestPanel(wx.Panel):
"""Simple test panel"""

def __init__(self, parent, id, **kwargs):
wx.Panel.__init__(self, parent,size=(200,400))
self.Bind(wx.EVT_LEFT_DOWN, self.onMouseClick)
self.Show(True)

def onMouseClick(self, event):
print "Defined in TestPanel"


if __name__ == "__main__":
app = wx.PySimpleApp()
foo = TestFrame(None, -1, title="Testing Panels", size=(200,200))
app.MainLoop()


If the event is caught in the parent frame, it fires the handler
defined in TestFrame, but NOT the handler in TestPanel. When I comment
the event binding in TestFrame.__init__(), the handler
TestPanel.onMouseClick() is fired.

If this works for you, then the problem is somewhere else in the app.
Regards,
Joel

Robin Dunn

unread,
Jan 8, 2010, 1:33:34 AM1/8/10
to wxpytho...@googlegroups.com

Is there some other child window that is covering the panel? If so you
should be binding to that window as it is the one that is getting the
events. Otherwise check if you are already intercepting the events from
the panel in some other binding.

Reply all
Reply to author
Forward
0 new messages