我在这里有一个小演示:
来自
DemoApp.py
:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import DragBehavior
from kivy.core.window import Window
class DraggableButton(DragBehavior, Button):
def on_drag(self, *args):
...
class DemoLayout(FloatLayout):
def __init__(self, **kwargs):
super(DemoLayout, self).__init__(**kwargs)
self.add_widget(DraggableButton(text='Drag Me'))
class DemoApp(App):
def build(self):
self.button= DraggableButton(text='Drag Me')
Window.bind(mouse_pos=lambda w, p: setattr(self.button, "mouse_pos", p))
return self.button
if __name__ == '__main__':
DemoApp().run()
来自
demo.kv
:
<DraggableButton>:
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000000
drag_distance: 0
问题是,我想在拖动DraggableButton
时调用我的on_drag
方法。
此外,我只想要在功能中的鼠标位置,因此我可以删除Window.bind(mouse_pos=lambda w, p: setattr(self.button, "mouse_pos", p))
行
有什么解决办法吗?
它使用DragBehavior
,所以我找到了DragBehavior的源代码
,并且我看到它有方法on_touch_move(touch)
。
我在DraggableButton()
中替换了这个方法,当按钮被移动时,它会得到MouseMotionEvent
和pos
,这似乎是鼠标的位置。
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import DragBehavior
from kivy.core.window import Window
class DraggableButton(DragBehavior, Button):
def on_touch_move(self, touch):
super().on_touch_move(touch) # run original `on_touch_move()`
print('[on_drag] touch:', touch)
#print('[on_drag] touch.dpos:', touch.dpos)
#print('[on_drag] touch.spos:', touch.spos)
print('[on_drag] touch.pos:', touch.pos)
print('---')
class DemoApp(App):
def build(self):
self.button = DraggableButton(text='Drag Me')
return self.button
if __name__ == '__main__':
DemoApp().run()
结果:
[on_drag] touch: <MouseMotionEvent spos=(0.36, 0.5766666666666667) pos=(288.0, 346.0)>
[on_drag] touch.pos: (288.0, 346.0)
---
[on_drag] touch: <MouseMotionEvent spos=(0.35875, 0.5783333333333334) pos=(287.0, 347.0)>
[on_drag] touch.pos: (287.0, 347.0)
---
[on_drag] touch: <MouseMotionEvent spos=(0.35875, 0.5783333333333334) pos=(287.0, 347.0)>
[on_drag] touch.pos: (287.0, 347.0)
---