在 wxpython 中检测拖动

问题描述

顾名思义,我试图在 wxpython 中运行一个函数,该函数在鼠标点击和拖动时激活并能够检测到拖动方向。从我的研究中我看到它是一个 wx.MouseEvent,但我没有找到任何实际的例子来帮助我理解如何使用它。有人可以帮忙吗?

解决方法

这是一个简单的例子:

import wx
class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None,-1,pos=(300,150),size=(320,250))
        self.panel = wx.Panel(self)
        self.text = wx.StaticText(self.panel,label="Left click mouse,move and release")
        self.panel.Bind(wx.EVT_LEFT_DOWN,self.OnDown)
        self.panel.Bind(wx.EVT_LEFT_UP,self.OnUp)
        self.panel.Bind(wx.EVT_MOTION,self.OnDrag)
        self.Show()

    def OnDown(self,event):
        x,y = event.GetPosition()
        print("Click coordinates: X=",x," Y=",y)

    def OnUp(self,y = event.GetPosition()
        print("Release coordinates: X=",y)

    def OnDrag(self,y = event.GetPosition()
        if not event.Dragging():
            event.Skip()
            return
        event.Skip()        
        #obj = event.GetEventObject()
        #sx,sy = obj.GetScreenPosition()
        #self.Move(sx+x,sy+y)
        print("Dragging position",y)
        

app = wx.App()
window = MyFrame()
app.MainLoop()