将FloatLayout放置在BoxLayout内

问题描述 投票:0回答:1

我正在尝试将浮动版式放置在Boxlayout内。当我尝试此操作时,内部的标签会相互堆叠。我究竟做错了什么?谢谢!

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label


def add_entry(bl):
    fl = FloatLayout()

    # add label left
    _lbl = Label()
    _lbl.id = '_lbl0'
    _lbl.text = 'LEFT'
    _lbl.pos_hint = {'x': 0, 'center_y': .5}
    fl.add_widget(_lbl)

    # add label center
    _lbl1 = Label()
    _lbl1.id = '_lbl1'
    _lbl1.text = 'CENTER'
    _lbl1.pos_hint = {'center_x': .5, 'center_y': .5}
    fl.add_widget(_lbl1)

    # add label right
    _lbl2 = Label()
    _lbl2.id = '_lbl2'
    _lbl2.text = 'RIGHT'
    _lbl2.pos_hint = {'right': 1, 'center_y': .5}
    fl.add_widget(_lbl2)

    bl.add_widget(fl)


class MyApp(App):

    def build(self):
        bl = BoxLayout()
        bl.orientation = 'vertical'
        for g in range(3):
            add_entry(bl)
        return bl

if __name__ == '__main__':
    MyApp().run()

我认为原因是FloatLayout的大小。尺寸似乎为0:这可以解释为什么标签彼此重叠。

这就是我想要的样子:That's how I want it to look like: 

这就是它的外观:That's how it appears:

position kivy boxlayout
1个回答
0
投票

您只需要向每个size_hint_x添加一个Label。类似于:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label


def add_entry(bl):
    fl = FloatLayout()

    # add label left
    _lbl = Label()
    _lbl.id = '_lbl0'
    _lbl.text = 'LEFT'
    _lbl.size_hint_x = 0.3
    _lbl.pos_hint = {'x': 0, 'center_y': .5}
    fl.add_widget(_lbl)

    # add label center
    _lbl1 = Label()
    _lbl1.id = '_lbl1'
    _lbl1.text = 'CENTER'
    _lbl1.size_hint_x = 0.3
    _lbl1.pos_hint = {'center_x': .5, 'center_y': .5}
    fl.add_widget(_lbl1)

    # add label right
    _lbl2 = Label()
    _lbl2.id = '_lbl2'
    _lbl2.text = 'RIGHT'
    _lbl2.size_hint_x = 0.3
    _lbl2.pos_hint = {'right': 1, 'center_y': .5}
    fl.add_widget(_lbl2)

    bl.add_widget(fl)


class MyApp(App):

    def build(self):
        bl = BoxLayout()
        bl.orientation = 'vertical'
        for g in range(3):
            add_entry(bl)
        return bl

if __name__ == '__main__':
    MyApp().run()

[默认size_hint(1, 1),因此每个Label都会尝试填充FloatLayout的整个宽度。通过将size_hint_x设置为0.3,每个Label仅占据宽度的三分之一。

© www.soinside.com 2019 - 2024. All rights reserved.