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?
I've added an additional code snippet. I'm running the code on macOS with Python version 3.10, wxPython version 4.2.1, and PyOpenGL version 3.1.7.
import wx
from wx import glcanvas
from OpenGL.GL import *
import random
class MyCanvas(glcanvas.GLCanvas):
def __init__(self, parent):
glcanvas.GLCanvas.__init__(self, parent, -1)
self.context = glcanvas.GLContext(self)
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
def on_left_down(self, event):
x, y = event.GetPosition()
print(f"Mouse clicked at ({x}, {y})")
def on_paint(self, event):
dc = wx.PaintDC(self)
self.SetCurrent(self.context)
self.on_draw()
def on_draw(self):
glClearColor(random.randrange(256)/255, random.randrange(256)/255, random.randrange(256)/255, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
self.SwapBuffers()
def main():
app = wx.App()
frame = wx.Frame(None)
sizer = wx.BoxSizer()
frame.SetSizer(sizer)
canvas = MyCanvas(frame)
sizer.Add(canvas, 1, wx.EXPAND)
frame.Show()
app.MainLoop()
if __name__ == '__main__':
main()
I suspect the issue is whatever you're using to run the code in debug mode.
Run this from the command line, avoiding that debug/development tool.
Running on Linux: