left/right mouse button events not trapped

M

mitsura

Hi,

I have included a small listing. The test program opens a panel and
show a bitmap.
What I want is to when the mouse is over the bitmap panel, I want to
trap the left mouse click.
The purpose is to get the position of the mouse pointer on the bitmap.
However, for some reason, the left (I also tried right) mouse clicks
are not intercepted.
I new to Python and wxWindows so any help would be greatly appreciated.

With kind regards,

Kris
"
import string
import wx
import images
import os
import cStringIO
import xml.sax

from wxPython.wx import *
from Main import opj

class DisplayPicture(wx.Frame):
cD = 0

def __init__(self, parent, id, title, bmp, w, h):
wxFrame.__init__(self,parent,wxID_ANY, title, size = ( w, h),
style=wxDEFAULT_FRAME_STYLE)

mD = 0

self.Panel=wx.Panel(self)
wx.StaticBitmap(self.Panel, -1, bmp, (5, 5) )

""" FOR SOME REASON THE MOUSE CLICKS DON'T WORK!"""
""" SHIT SHIT SHIT SHIT ... x 1000 """
wx.EVT_LEFT_DOWN(self.Panel, self.OnLeftClick)
wx.EVT_LEFT_UP(self.Panel, self.OnLeftClick)
wx.EVT_LEFT_DCLICK(self.Panel, self.OnLeftClick)


self.CreateStatusBar()
self.Show()

self.SetStatusText('Submap Coordinates:')

def OnLeftClick(self, event):
print "ok the mouse click works!"

class MyApp(wx.App):
cD = 0
def OnInit(self):
wx.InitAllImageHandlers()
return 1

#
# Main
#

if __name__ == "__main__":
app = MyApp(0)


data = open(opj('c:\winnt\Greenstone.bmp'), "rb").read()
stream = cStringIO.StringIO(data)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
bmpW = bmp.GetWidth()
bmpH = bmp.GetHeight()
DisplayPicturePanel=DisplayPicture(None, -1, "Test MB", bmp, bmpW,
bmpH)
#MainFrame = DisplayPicture(None,-1,"Test Mouse Clicks")


app.MainLoop()
"
 
M

mitsura

Thanks Claudio! This really really made my day. I have been searching
for the problem for hours! Thanks again.
 
C

Claudio Grondi

I don't know much about wxPython, but
the left/right mouse button events _ARE_ trapped, but
not when clicking on the image, probably because they
are registered to the panel not to the image.
Just resize the window and click next to the image.

Hope this helps for now, before you get a response
from someone who knows some more about wxPython
(e.g. why the events from the bitmap are not forwarded
to the panel containing it?).

Claudio
P.S. Here the corrected code I was able to run on my machine:

import string
import wx
# import images
import os
import cStringIO
import xml.sax

from wxPython.wx import *
# from Main import opj

class DisplayPicture(wx.Frame):
cD = 0
def __init__(self, parent, id, title, bmp, w, h):
wxFrame.__init__(self, parent, wxID_ANY, title, size = ( w, h),
style=wxDEFAULT_FRAME_STYLE )
mD = 0

self.Panel=wx.Panel(self)
wx.StaticBitmap(self.Panel, -1, bmp, (5, 5) )

""" FOR SOME REASON THE MOUSE CLICKS DON-T WORK!"""
""" SHIT SHIT SHIT SHIT ... x 1000 """
wx.EVT_LEFT_DOWN(self.Panel, self.OnLeftClick)
wx.EVT_LEFT_UP(self.Panel, self.OnLeftClick)
wx.EVT_LEFT_DCLICK(self.Panel, self.OnLeftClick)

self.CreateStatusBar()
self.Show()

self.SetStatusText('Submap Coordinates:')

def OnLeftClick(self, event):
print "ok the mouse click works!"

class MyApp(wx.App):
cD = 0
def OnInit(self):
wx.InitAllImageHandlers()
return 1

#
# Main
#

if __name__ == "__main__":
app = MyApp(0)


data = open('e:\winnt\Granit.bmp', "rb").read()
stream = cStringIO.StringIO(data)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
bmpW = bmp.GetWidth()
bmpH = bmp.GetHeight()
DisplayPicturePanel=DisplayPicture(None, -1, "Test MB", bmp, bmpW, bmpH)
#MainFrame = DisplayPicture(None,-1,"Test Mouse Clicks")


app.MainLoop()
 
C

Claudio Grondi

Below code which does the trick :))

Claudio

import string
import wx
# import images
import os
import cStringIO
import xml.sax

from wxPython.wx import *
# from Main import opj

class DisplayPicture(wx.Frame):
cD = 0
def __init__(self, parent, id, title, bmp, w, h):
wxFrame.__init__(self, parent, wxID_ANY, title, size = ( w, h),
style=wxDEFAULT_FRAME_STYLE )
mD = 0

self.Panel = wx.Panel(self)
self.Bitmap = wx.StaticBitmap(self.Panel, -1, bmp, (5, 5) )

""" FOR SOME REASON THE MOUSE CLICKS DON-T WORK!"""
""" SHIT SHIT SHIT SHIT ... x 1000 """
wx.EVT_LEFT_DOWN(self.Panel, self.OnLeftClickDown)
wx.EVT_LEFT_UP(self.Panel,self.OnLeftClickUp)
wx.EVT_LEFT_DCLICK(self.Panel, self.OnLeftDoubleClick)

wx.EVT_LEFT_DOWN(self.Bitmap, self.OnLeftClickDownOnBitmap)

self.CreateStatusBar()
self.Show()

self.SetStatusText('Submap Coordinates:')

def OnLeftClickDown(self, event):
print " OnLeftClickDown"

def OnLeftClickDownOnBitmap(self, event):
print " OnLeftClickDownOnBitmap"

def OnLeftClickUp(self, event):
print " OnLeftClickUp"

def OnLeftDoubleClick(self, event):
print " OnLeftDoubleClick"

class MyApp(wx.App):
cD = 0
def OnInit(self):
wx.InitAllImageHandlers()
return 1

#
# Main
#

if __name__ == "__main__":
app = MyApp(0)


data = open('e:\winnt\Granit.bmp', "rb").read()
stream = cStringIO.StringIO(data)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
bmpW = bmp.GetWidth()
bmpH = bmp.GetHeight()
DisplayPicturePanel=DisplayPicture(None, -1, "Test MB", bmp, bmpW, bmpH)
#MainFrame = DisplayPicture(None,-1,"Test Mouse Clicks")

app.MainLoop()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top