是否有必要在`Widget`中添加`FloatLayout`?

时间:2013-09-26 22:57:10

标签: python widget kivy

如果我有一个我在Python源文件中定义为Widget的孩子的类,我是否需要添加FloatLayout个孩子,或者我可以将元素放在直接Widget,根本不使用FloatLayout

# Python source
class FooBar(Widget):
    pass

# Kivy source
<FooBar>:
    FloatLayout:  # Is this necessary? 
        SomeChildWidget:
            ...
        AnotherChildWidget:
            ...

2 个答案:

答案 0 :(得分:0)

快速查看Kv language documentation表示不,您不需要窗口小部件内的布局。考虑一下他们给出的例子under the template section

<MyWidget>:
    Button:
        text: "Hello world, watch this text wrap inside the button"
        text_size: self.size
        font_size: '25sp'
        markup: True
    Button:
        text: "Even absolute is relative to itself"
        text_size: self.size
        font_size: '25sp'
        markup: True
    Button:
        text: "Repeating the same thing over and over in a comp = fail"
        text_size: self.size
        font_size: '25sp'
        markup: True

此处有3个按钮小部件直接放在<MyWidget>声明下。

答案 1 :(得分:0)

在小部件中嵌入小部件没有任何问题。您可以完全控制位置和尺寸。

add_widget方法(和children property)是Widget类的一部分,而Layout只是继承自Widget。例如,Kivy pong tutorial没有任何布局。

注意:如果您开始想知道使用WidgetFloatLayout的区别。基本上,后者尊重Widget.pos_hint和Widget.size_hint属性。它允许您使用比例值来定位Widget的大小。