【发布时间】:2016-09-04 09:24:39
【问题描述】:
我用一个名为 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端,DTErrorBar类是这样声明的:
class DTErrorBar(Widget):
pass
创建了从基础 Widget 类派生的自定义小部件。更改为从 Layout 类继承解决了它:
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 是什么样的小部件?
-
这是我正在创建的自定义小部件,问题中的第一个代码块显示了它是如何定义的。那里可能有问题。
-
如果不是布局类型的小部件,那么它的所有内容都将放在 pos=[0,0] 处。
-
哈哈,我现在才弄清楚,实际上,它是从 Widget 派生的,通过从 RelativeLayout 派生它得到修复,我也可以在我的自定义小部件中删除父 RelativeLayout。您要发布答案吗?
-
是的,我会发布一个答案,以便将来的读者更容易阅读。
标签: widget position kivy kivy-language