关于kivy事件调度行为的问题

2024-05-14 09:01:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个测试python片段:

py文件:

from kivy.app import App
class tutorApp(App):

    def bn_pressed(self,button):
        print(button.text)
        return True

app = tutorApp()
app.load_kv('tutorial-3.kv')
app.run()

和kv文件(tutorial-3.kv):

<Tb@Button>:
    on_touch_down:app.bn_pressed(self)


BoxLayout:
    Tb:
        text:'button1'
    Tb:
        text:'button2'
    Tb: 
        text:'button3'

当我从左到右按这三个按钮时,我得到:

# press left
button3
button2
button1
# press middle
button3
button2
# press right
button3

我换了句话:

<Tb@Button>

<Tb@Lable>

再来一次,我得到:

# press left label
button3
button2
button1
# press middle label
button3
button2
button1
# press right label
button3
button2
button1

造成这种差异的原因是什么?假设在事件处理程序中renturn True时停止事件传播,为什么会调用keep get?你知道吗

谢谢你的帮助。你知道吗

顺便说一句,如果我将事件更改为on\u press,一切正常,当按下三个按钮中的任何一个时,只调用正确的处理程序和正确的处理程序。你知道吗


Tags: 文件textapp处理程序事件tblabelpress
2条回答

on_touch_down方法必须返回True才能停止传播。在kv中使用的on_touch_down:并不替换此方法或任何其他python代码,而是附加到on_touch_down事件。这意味着您可以在同一kv规则中多次写入on_touch_down:,并且所有绑定仍然有效。你知道吗

标签文档说明如果返回True,传播将停止。我建议你写一份错误报告。我看到了你在这个例子中所做的同样的事情。你知道吗

https://kivy.org/doc/stable/api-kivy.uix.label.html

on_touch_down(touch)

bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.

相关问题 更多 >

    热门问题