是否需要在“Widget”中添加“FloatLayout”?

2024-04-27 04:09:16 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我在Python源文件中定义了一个类作为Widget的子类,那么我是否需要添加一个FloatLayout子类,或者我可以直接将元素放置在Widget中,而不使用FloatLayout?你知道吗

# Python source
class FooBar(Widget):
    pass

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

Tags: 元素source定义ispasswidgetthis子类
2条回答

在小部件中嵌入小部件没有错。您完全可以控制位置和大小。你知道吗

^{}方法(和children property)是^{}类的一部分,而^{}只是从Widget继承的。例如,Kivy pong tutorial没有任何布局。你知道吗

注意:如果您开始怀疑使用Widget^{}的区别。基本上,后者是荣誉Widget.pos\u提示以及Widget.size\u提示属性。它允许您使用与Widget大小成比例的值进行定位。你知道吗

快速浏览一下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>声明下面。你知道吗

相关问题 更多 >