【发布时间】:2021-06-16 20:41:40
【问题描述】:
我正在从一本涉及制作绘画应用程序的书中学习 kivy。作者有一次在画布类(继承自 Widget)上介绍了新按钮,并说我们必须检查应用程序在画布上收到的点击是否也位于其子之一上。在这种情况下,我们会忽略前者而对后者采取行动。 我想通过遍历 self.children 来实现这一点,检查 on_touch_down(touch) 成员函数是否为任何孩子返回 true,如果是则从函数返回:
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)
但是,我不遵循并且想了解他的速记语法:
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)
任何清晰度表示赞赏。谢谢!
【问题讨论】:
-
它不检查任何子小部件,它只是检查自己。该行显式调用
on_touch_down()方法的基类版本。
标签: python inheritance kivy children