How to capture mouse events on a opengl canvas?

Hi there,

I am currently working with a wxPython GUI window that incorporates an OpenGL canvas panel. My goal is to render icons on this canvas and enable mouse interactions, specifically allowing users to click on the rendered icons. I have a couple of questions regarding this process:

  • Mouse Event Capture: How can I effectively capture mouse events on an OpenGL canvas within a wxPython GUI?
  • Custom Mouse Cursor: Additionally, I am interested in changing the mouse cursor dynamically when it moves over one of the icons. Could you provide guidance on implementing this feature?

Thank you!

In fact, I am using OpenGL to render 3D models and require the inclusion of icons to facilitate mouse interactions. For example, clicking on one of the icons will trigger a change in the model’s action.

Hi @caol64,

What do you mean by “effectively”?
If you are using a subclass of wx.glcanvas.GLCanvas, you can capture the desired mouse events like:

>>> self.Bind(wx.EVT_LEFT_DOWN, ...)

You can also change the cursor like:

 >>> self.SetCursor(wx.Cursor(wx.CURSOR_HAND))

Thanks, I’ll give this solution a try.

I’ve created a wxPython application and placed a GLCanvas in the wxFrame. I’ve bound the EVT_LEFT_DOWN event to the GLCanvas, but when running the program, the mouse click events don’t seem to respond in real-time. I have to wait until the program is closed to see that the clicked events are printed together. It feels like the main thread’s mainLoop is blocking the mouse click events. How can I make adjustments to address this, and do you have any suggestions?

Have you tried “wxdemo/Miscellaneous/GLCanvas”?
You can see how to handle mouse events there.

P.S. I noticed that the [Cube] window is broken. I’ll report this to the bug tracker.

@komoto48g The OP cross posted on StackOverflow multithreading - Mouse click events are being blocked when using wxPython with a GLCanvas - Stack Overflow
I suggested the same thing there and provided a working answer.

Twenty years ago, when I was a young useless c/c++ programmer, I made a library based on OpenGL/glut for my purposes (to learn GL for fun). Recently, I converted it to the wxPython package for educational purposes.

This package demonstrates how the FSM framework is used to define keys and mouse “effectively”.

:memo: “Effective” is my humble opinion that means readability and ease of customization, not memory usage or processing speed. :slightly_smiling_face:

@komoto48g @rolfofsaxony Thanks for the suggestions from both of you. I have found a solution and successfully executed it.