Question

I'm a newbie trying to throw together a small test program in which I have an EVT_LEFT_DOWN event bound to a StaticBitmap like so:

wx.EVT_LEFT_DOWN(imageBitmap, image_click_handler)

image_click handler looks something like this:

image_click_handler(self):
    self.Skip()
    while self.LeftIsDown():
       Do something

Basically, I'm trying to implement dragging when I click on the image. However, the self.LeftIsDown() method never returns false, which causes an infinite loop.

I feel like I'm doing something really silly. I've been following the documentation here (http://www.wxpython.org/docs/api/wx.MouseEvent-class.html), which is for an older version of WxPython (I'm using 3.0.0.0). Perhaps that is an issue?

Was it helpful?

Solution

wxPython is an event driven environment. Your event handler gets called when someone presses the left mouse button, and until the event handler finishes no other events will get through to your program. Your program has no means of becoming aware of the release of the button since all new events are stuck in a queue waiting for your handler to return. In general in event handlers it is dangerous to use anything resembling an infinite loop.

The solution is typically in several parts: (1) set up a timer to send periodic events but don't start it yet. (2) In your init_click_handler you start this timer and return. (3) Write a timer event handler where you "Do something" once per event. (4) When you get a EVT_LEFT_UP you stop the timer (another event handler), or alternatively you can check LeftIsDown() within the timer event handler and stop the timer when it's False.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top