Kivy 自定义小部件不堆叠

Kivy custom widget not stacking

我用一个名为 DTErrorBar 的自定义小部件制作了一个示例布局,

<DTErrorBar>:
    RelativeLayout:
        size_hint: None, None
        size: 20, 450
        canvas:
            Color:
                rgba: col_fg
            Line:
                points: 10, 10, 10, 410
                width: 4
            Color:
                hsv: col_gn_marker_hsla
            Line:
                points: 10, 10, 10, 410
                width: 2

在我的示例中,此小部件包含在这样的 BoxLayout 中:

    BoxLayout:
        orientation: "horizontal"
        DTErrorBar:
        Button:
        Button:
        DTErrorBar:

但我无法让它与按钮和 boxLayout 内的其他 DTErrorBar 堆叠在一起,这就是我得到的:

这就是我所期望的:

现在,您可能会问,我是如何获得我所期望的图像的?它是通过对我使用自定义小部件的代码进行少量更改而制作的:

    BoxLayout:
    orientation: "vertical"
    BoxLayout:
        orientation: "horizontal"
        RelativeLayout:
            DTErrorBar:
        Button:
        Button:
        RelativeLayout:
            DTErrorBar:

因此,将 DTErrorBar 小部件包装在 RelativeLayout 中似乎可以使其工作,但我不确定为什么,如果我已经有一个 RelativeLayout 作为我的 DTErrorBar 小部件内所有内容的包装器。

总而言之,我必须对 DTErrorBar 的定义做些什么才能获得预期的行为?如果有人可以解释我在这里犯了什么错误以便从我的错误中吸取教训,那也会很有趣,谢谢。

非预期情况的完整代码:https://gist.github.com/payala/bc54f4d3d2378a97c26c9afe88858b07

非预期情况的完整代码:https://gist.github.com/payala/9de4de166c7c1942cc923c32550bf661

编辑:在jligeza的回答后修改

在python这边,DTErrorBarclass是这样声明的:

class DTErrorBar(Widget):
    pass

正在创建从基础 Widget class 派生的自定义小部件。更改为继承布局 class 解决了它:

class DTErrorBar(RelativeLayout):
    pass

通过这样做,在小部件定义中包含 RelativeLayout 也不再有意义,因为小部件本身就是一个 RelativeLayout,因此最终的小部件定义代码变为:

<DTErrorBar>:
    size_hint: None, None
    size: 20, 450
    canvas:
        Color:
            rgba: col_fg
        Line:
            points: 10, 10, 10, 410
            width: 4
        Color:
            hsv: col_gn_marker_hsla
        Line:
            points: 10, 10, 10, 410
            width: 2

DTErrorBar 应该子类化一些布局小部件,例如 RelativeLayout。如果你子类化一个简单的 Widget,那么它不知道如何定位它的 children(这里:相对布局与 canvas),所以它将它们的所有位置设置为 window 的 [0, 0].