1

I'm playing with wxPython event bindings in order to make a dragging algorithm. However I've encountered a problem, when the mouse is not directly over my frame the event doesn't trigger.

This becomes a problem while dragging seeing as if the mouse escapes the frame (like if the user moved it quickly), the frame neglects to update it's position.

Is there anyway to change the bindings so that they trigger even if the mouse isn't over the frame in question?

Snippet:

    self.Bind(wx.EVT_LEFT_DOWN, self.relative_mouse_position)
    self.Bind(wx.EVT_LEFT_UP, self.wid_unbind)

Snippet:

def relative_mouse_position (self, event):
    cx, cy = wx.GetMousePosition()
    x, y = self.GetPosition()

    RelX = cx - x
    RelY = cy - y

    self.Bind(wx.EVT_MOTION, lambda event: self.wid_drag(event, RelX, RelY))


def wid_drag (self, event, RelX, RelY):
    cx, cy = wx.GetMousePosition()

    x = cx - RelX
    y = cy - RelY

    if x < 0:
        x = 0

    if y < 0:
        y = 0

    self.SetPosition((x, y))

def wid_unbind (self, event):
    self.Unbind(wx.EVT_MOTION)

2 Answers 2

2
+100

When you start a drag, call CaptureMouse to keep the mouse locked to the window that you're dragging.

Sign up to request clarification or add additional context in comments.

Comments

0

Not tested but probably bind, EVT_LEAVE_WINDOW to trigger when mouse is outside window.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.