【问题标题】:Propagating events in Kivy在 Kivy 中传播事件
【发布时间】:2020-05-08 16:56:58
【问题描述】:

我在tutorial 的末尾稍微修改了这个例子:

#!/usr/bin/env python

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty


class RootWidget(BoxLayout):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.add_widget(Button(text='button 1'))
        cb = CustomBtn()
        cb.bind(pressed=self.btn_pressed)
        self.add_widget(cb)
        self.add_widget(Button(text='button 2'))

    def btn_pressed(self, instance, pos):
        print(f'4444 RootWidget.btn_pressed, instance: {instance} pos: {pos}')


class CustomBtn(Widget):
    pressed = ListProperty([0, 0])

    def on_touch_down(self, touch):
        print(f'1111 CustomButton.on_touch_down, touch id: {id(touch)}, touch {touch}')
        if self.collide_point(*touch.pos):
            print(f'2222 CustomButton.on_touch_down, collision, self.pressed: {self.pressed}')
            self.pressed = touch.pos
            # we consumed the touch. return False here to propagate
            # the touch further to the children.
            return True
        return super(CustomBtn, self).on_touch_down(touch)

    def on_pressed(self, instance, pos):
        print(f'3333 CustomButton.on_pressed, instance: {instance} pos: {pos}')


class TestApp(App):

    def build(self):
        return RootWidget()


if __name__ == '__main__':
    TestApp().run()

现在,当我按button 1 时,我得到了这个:

1111 CustomButton.on_touch_down, touch id: 140523635041336, touch <MouseMotionEvent spos=(0.1875, 0.31999999999999995) pos=(150.0, 191.99999999999997)>

为什么?这个应该在Button(text='button 1')处理吧?

当然,由于collide_point 调用检测到此触摸发生在自定义按钮区域之外,这不会适得其反,但为什么会首先发生这种情况?

接下来,为了让这更奇怪,当我点击 button 2 时,这并没有发生(即没有打印任何内容)。

为什么(以及如何?)在此处分派事件?

基维:1.11.1。 操作系统:Debian 10(破坏者)amd64 图形用户界面:KDE 等离子。 Python:3.7。

启动日志:

[INFO   ] [Logger      ] Record log in /home/user/.kivy/logs/kivy_20-05-08_16.txt
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "/home/user/projects/kvapp/venv37/lib/python3.7/site-packages/kivy/__init__.py"
[INFO   ] [Python      ] v3.7.2 (default, Feb 22 2019, 18:56:07) 
[GCC 6.3.0 20170516]
[INFO   ] [Python      ] Interpreter at "/home/user/projects/kvapp/venv37/bin/python"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] Backend used <sdl2>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 NVIDIA 418.43'>
[INFO   ] [GL          ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO   ] [GL          ] OpenGL renderer <b'GeForce GTX 1080 Ti/PCIe/SSE2'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 NVIDIA'>
[INFO   ] [GL          ] Texture max size <32768>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available

【问题讨论】:

    标签: python python-3.x debian kivy kde-plasma


    【解决方案1】:

    事件被传播到Window 中的所有Widgets,直到其中任何一个停止传播。为每个Button 调用on_touch_down() 方法的顺序与添加它们的顺序相反,即:

    1. 按钮 2
    2. 自定义按钮
    3. 按钮 1

    如果触摸位置在按钮 2 上,则其 on_touch_down() 返回 True 并停止事件传播,因此其他 Widget 不会收到该事件。这就是为什么按下按钮 2 不会产生打印。

    当您单击按钮 1 时,按钮 2 会首先看到它,但由于它不在按钮 2 内,因此该事件被忽略并允许传播到 CustomBtnCustomBtn on_touch_down() 打印 1111 消息,但由于触摸在 CustomBtn 之外,它还允许继续传播。现在 Button 1 获取事件并处理它,停止传播。

    当您单击CustomBtn 时,按钮 2 再次首先看到它,但允许继续传播。然后CustomBtn看到事件,打印1111消息,确定触摸在CustomBtn上,所以打印2222消息,修改pressed属性(导致on_pressed()方法被触发),绑定的btn_pressed()方法也被触发。然后停止传播(通过True 返回),因此按钮 1 永远不会看到此事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多