0

我在本教程的最后修改了这个例子:

#!/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_pointcall 检测到此触地发生在自定义按钮区域之外,这不会适得其反,但为什么会发生这种情况呢?

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

为什么(以及如何?)在此处发送事件?

基维:1.11.1。操作系统:Debian 10 (buster) amd64 GUI:KDE Plasma。蟒蛇: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
4

1 回答 1

0

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

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

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

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

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

于 2020-05-08T20:03:53.673 回答