在 kivy 中检查 child 交互的语法
Syntax for checking child interactions in kivy
我正在从一本涉及制作绘画应用程序的书中学习 kivy。作者曾在 canvas class(继承自 Widget)上介绍了新按钮,并说我们必须检查应用程序在 canvas 上收到的点击是否也位于其children之一。在那种情况下,我们会忽略前者而对后者采取行动。
我想通过循环 self.children 来实现这一点,检查 on_touch_down(touch) 成员函数 returns 是否对任何 child 为真,如果是则从函数返回案例:
class CanvasWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.my_color = get_color_from_hex('#FF0000')
self.line_width=2
def on_touch_down(self, touch):
for child in self.children:
if(child.on_touch_down(touch)):
return
with self.canvas:
Color(rgba=self.my_color)
touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=self.line_width)
这很好用。
然而,我不理解并想了解他的 shorthand 语法:
class CanvasWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.my_color = get_color_from_hex('#FF0000')
self.line_width=2
def on_touch_down(self, touch):
#I don't understand the following line
if Widget.on_touch_down(self, touch):
return
#Widget refers to the parent class (I hope?) how does it check with the
#children?
with self.canvas:
Color(rgba=self.my_color)
touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=self.line_width)
任何清晰度表示赞赏。谢谢!
Widget
class 是所有 kivy 小部件的基础 class,它处理 touch
事件到其 children 的传播它的 on_touch_down()
方法。与其直接引用 Widget
基 class,更好的方法是使用 super()
方法,如下所示:
if super(CanvasWidget, self).on_touch_down(touch):
return True
如果children中的任何一个想要停止传播,returnsTrue
(停止传播)将进行传播。 Widget
中执行此操作的代码是:
for child in self.children[:]:
if child.dispatch('on_touch_down', touch):
return True
由于它不直接引用 Widget
基 class,如果您决定更改 [=21= 的基 class,则 super()
方法更安全].
我正在从一本涉及制作绘画应用程序的书中学习 kivy。作者曾在 canvas class(继承自 Widget)上介绍了新按钮,并说我们必须检查应用程序在 canvas 上收到的点击是否也位于其children之一。在那种情况下,我们会忽略前者而对后者采取行动。 我想通过循环 self.children 来实现这一点,检查 on_touch_down(touch) 成员函数 returns 是否对任何 child 为真,如果是则从函数返回案例:
class CanvasWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.my_color = get_color_from_hex('#FF0000')
self.line_width=2
def on_touch_down(self, touch):
for child in self.children:
if(child.on_touch_down(touch)):
return
with self.canvas:
Color(rgba=self.my_color)
touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=self.line_width)
然而,我不理解并想了解他的 shorthand 语法:
class CanvasWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.my_color = get_color_from_hex('#FF0000')
self.line_width=2
def on_touch_down(self, touch):
#I don't understand the following line
if Widget.on_touch_down(self, touch):
return
#Widget refers to the parent class (I hope?) how does it check with the
#children?
with self.canvas:
Color(rgba=self.my_color)
touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=self.line_width)
任何清晰度表示赞赏。谢谢!
Widget
class 是所有 kivy 小部件的基础 class,它处理 touch
事件到其 children 的传播它的 on_touch_down()
方法。与其直接引用 Widget
基 class,更好的方法是使用 super()
方法,如下所示:
if super(CanvasWidget, self).on_touch_down(touch):
return True
如果children中的任何一个想要停止传播,returnsTrue
(停止传播)将进行传播。 Widget
中执行此操作的代码是:
for child in self.children[:]:
if child.dispatch('on_touch_down', touch):
return True
由于它不直接引用 Widget
基 class,如果您决定更改 [=21= 的基 class,则 super()
方法更安全].