kivy如何响应点击事件?

How does kivy response to a click event?

我正在尝试使用 kivy 和 python 开发图灵机模拟器,并且我获得了基本的 GUI 设置。但是,当我单击顶部栏和侧栏中的按钮时,on_touch_down() 函数有点重载 on_press() 函数。我该如何解决? 所以我想要的是当我点击舞台区域的任何地方时,调用 on_touch_down() 函数并打印坐标,当我点击任何按钮时调用 on_press() 函数但什么也没有得到打印。 这是我的代码:

GUIDisplay.py

from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import *

class RootWidget(FloatLayout):

    def add_transition_toggle(self):
        print 'something happened'

    def change_tape(self):
        pass

    def backgroundpress(self):
        print 'region'

    def actionbuttonpress(self):
        print 'menu'
    def on_touch_down(self, touch):
        print touch.x, touch.y


class GUIDisplay(App):

    def build(self):
        app = RootWidget()
        return app


def start_app():
    GUIDisplay().run()

if '__main__' == __name__:
    start_app()

GUIDisplay.kv

<RootWidget>:
    # menu bar
    id: toolbars
    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                text: ''
                title: 'Turing App'
                with_previous: False
            ActionGroup:
                text: 'File'
                mode: 'spinner'
                size_hint_x: None
                width: 90
                ActionButton:
                    text: 'New'
                    on_press: root.actionbuttonpress()
                ActionButton:
                    text: 'Open'
                ActionButton:
                    text: 'Save'
                ActionButton:
                    text: 'Save As'
                ActionButton:
                    text: 'Quit'
            ActionGroup:
                text: 'Play'
                mode: 'spinner'
                size_hint_x: None
                width: 90
                ActionButton:
                    text: 'Play to End'

    # tool bar
    BoxLayout:

        BoxLayout:
            y: 10.0
            x: 10.0
            height: 400.0
            width: 80.0
            size_hint_y: 0.9
            size_hint_x: 0.08
            orientation: 'vertical'
            opacity: 0.5
            Button:
                text: 'Select'
            Button:
                text: 'Add State'
            Button:
                text: 'Modify State'
            Button:
                on_release: root.add_transition_toggle()
                on_press: pass
                text: 'Add Transition'
            Button:
                text: 'Tape'
                on_release: root.change_tape()
        Label:
            text: 'Stage Area'

我会第一个承认这很丑陋,所以也许其他人可以找到更好的方法。据我了解,问题是 RelativeLayout 正在消化触摸事件,因此不会将其传递到小部件树下。因此,您可以沿着这些方向尝试一些操作,以检查 children 是否已消化它(是否按下了按钮),如果没有,则打印 x、y 坐标。

def on_touch_down(self, touch):
    event_handled = False
    for child in self.children:
        if not event_handled:
            if child.dispatch('on_touch_down', touch):
                event_handled = True

    if not event_handled:
        print touch.x, touch.y

希望对您有所帮助...