我在本教程的最后修改了这个例子:
#!/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
call 检测到此触地发生在自定义按钮区域之外,这不会适得其反,但为什么会发生这种情况呢?
接下来,为了让这更奇怪,当我单击 时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