rectangletangle
rectangletangle

Reputation: 52911

wxPython Binding Problem

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)

Upvotes: 1

Views: 498

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308101

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

Upvotes: 2

YOU
YOU

Reputation: 123791

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

Upvotes: 0

Related Questions