首页 > 解决方案 > 2 BoxLayout 中的 FloatLayouts 格式不正确

问题描述

我是 Kivy 的新手,正在开发 UI 以使一组相当复杂的小部件正确显示。我的根小部件是一个 boxlayout(horiz),我试图让 2 个浮动布局并排出现。但是第二个浮动布局显示在第一个浮动布局的 boxlayout 的左侧。我在浮动布局级别尝试使用和不使用 pos_hints 和 size_hints,但无济于事。一些基本代码:

<BoxLayout>:
    id: rootwid
    orientation: 'horizontal'

    FloatLayout:
        id: leftside
        pos_hint: {'x':0, 'y':0} (Also tried 'right' and 'top' and commenting out the line)
        #size_hint: (.5, 1)
        (Buttons & Labels here, which lay out properly within the floatlayout)

    FloatLayout:
        id: rightside
        pos_hint: {'x': .5, 'y':0} (Also tried 'right' and 'top' and commenting out the line)
        #size_hint: (.5, 1)
        (Buttons & Labels here, which lay out properly within the floatlayout)

我错过了什么?谢谢!

标签: pythonkivykivy-language

解决方案


除非您将来使用 FloatLayouts,否则我建议您将它们替换为其他类型的布局 bcoz,因为它们是浮动的,它们往往独立于父级。

尝试使用 GridLayout 来容纳按钮和标签以及其他像这样的小部件;

BoxLayout:
    orientation: 'horizontal'
    GridLayout:
        cols: any
        rows: any
        # Left widgets added here

    GridLayout:
        cols: any
        rows: any
        # Right widgets added here


您也可以相应地嵌套它们,以扩展对这些运行KivyCatalog的洞察力,这是 kivy-examples 中的一个内置 python 文件,我认为
通常在 ~/.local/share/kivy-examples/demo/kivycatalog/main.py 对于
Windows的 Ubuntu检查 AppData 然后本地然后共享,默认情况下 AppData 是隐藏的,不要忘记这一点。

KivyCatalog 是交互式的,因为它会在您编写 kv 代码时显示您的更改,试试吧。


推荐阅读