0

我想用 Kivy 创建一个按钮小部件,它在单击时使用来自触摸事件的信息,即鼠标位置和用于单击的鼠标按钮。

如果我重新实现on_releaseon_press像这样:

from kivy.uix.button import Button

class myBtn(Button):
    def on_release(touch=None):
        print('Touch:', touch) # Touch: None (always)

触摸将永远是无。如果我重新实现on_touch_up或者on_touch_down我可以访问触摸信息:

from kivy.uix.button import Button

class myBtn(Button):
    def on_touch_up(touch=None):
        print('Touch:', touch)
        # Touch: <MouseMotionEvent spos=(..., ...) pos=(..., ...)

        print('Button:', touch.button) # Button: left

这个版本的问题是按钮按下/释放动画即使在我释放鼠标按钮后也会保持按下状态,而且该函数被调用 2 次而不是只调用一次。

如果我on_touch_down对函数执行相同操作,则仅执行一次,但单击时按钮动画根本不会改变。

我怎样才能恢复 MouseMotionEvent 避免我发现的问题on_touch_downon_touch_up

4

1 回答 1

0

如果你想使用on_release或者on_press你可以访问MouseMotionEvent这样的:

from kivy.uix.button import Button

class myBtn(Button):
    def on_release():
        print('Touch:', self.last_touch)
        print('Button:', self.last_touch.button)

如果你想使用on_touch_down或者on_touch_up你只需​​要确保调用 super() 实现:

from kivy.uix.button import Button

class myBtn(Button):
    def on_touch_up(touch):
        print('Touch:', touch)
        print('Button:', touch.button)    
        super(myBtn, self).on_touch_up(touch) 

但它仍然会被执行不止一次。

于 2015-08-11T18:48:07.517 回答