在基维传播事件
Propagating events in Kivy
我在这个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。
OS:Debian 10(克星)amd64
GUI: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
事件将传播到 Window
中的所有 Widgets
,直到其中任何一个停止传播。每个 Button
的 on_touch_down()
方法的调用顺序与添加它们的顺序相反,即:
- 按钮 2
- 自定义按钮
- 按钮 1
如果触摸位置在按钮 2 上,它的 on_touch_down()
returns 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
return),因此按钮 1 永远不会看到此事件。
我在这个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。 OS:Debian 10(克星)amd64 GUI: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
事件将传播到 Window
中的所有 Widgets
,直到其中任何一个停止传播。每个 Button
的 on_touch_down()
方法的调用顺序与添加它们的顺序相反,即:
- 按钮 2
- 自定义按钮
- 按钮 1
如果触摸位置在按钮 2 上,它的 on_touch_down()
returns 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
return),因此按钮 1 永远不会看到此事件。