Search code examples
wxpythonmouseevent

How can I receive mouse events on a wxPython StaticBitmap?


I have been attempting to receive mouse events on a wx.StaticBitmap object in wxpython. It has not gone well. I am using wxpython 4.1.0 with wxWidgets 3.1.4 on MacOS. I have even used the inspection tool to investiate what events are sent to the StaticBitmap object. It appears there are no mouse events at all arriving on the StaticBitmap object.

Here is some example code that demonstrates the problem:

import wx

def OnFrameClicked(e):
    print("Frame received click event")
    print(e.GetPosition())
    e.Skip()

def OnImageClicked(e):
    print("Image received click event")
    print(e.GetPosition())

app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "wxStaticBitmap Experiment")
frame.Bind(wx.EVT_LEFT_DOWN, OnFrameClicked)
bmp = wx.Image('100x100.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
image = wx.StaticBitmap(frame, bitmap=bmp)
image.Bind(wx.EVT_LEFT_DOWN, OnImageClicked)
frame.Show(True)
# uncomment the next 2 lines to enable detailed inspection
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
print(wx.version())
app.MainLoop()

A github project that also includes the example png image is here: wxpython-staticbitmap

How can I get mouse events when the StaticBitmap object is clicked?


Solution

  • It may be that wx.StaticBitmap is not a “real” widget on Mac but it’s simply painted on screen by the OS. If you need the events, I suggest you to look into the generic implementation of wx.StaticBitmap in wx.lib.statbmp.