python - 基维 : BoxLayout containing horizontal BoxLayout

标签 python user-interface kivy

我试图将 4 个水平盒子布局以垂直方式堆叠在 BoxLayout 中。

Rough Sketch

我的 KV 文件:

<HBoxWidget>:
    BoxLayout:
        size: root.size
        pos: root.pos
        id: boxlayout_h
        orientation: 'horizontal'
        Image:
            source: '/Users/driftking9987/Desktop/fp.gif'
<VBoxWidget1>:
    BoxLayout:
        spacing: 10
        orientation: "horizontal"
        size: [1,.25]
        pos: root.pos
        Label:
            text: "Status : "
            color: [0,84,80,19]
        Label:
            text: "Pending"
            color: [0,84,80,19]
        Widget:


<ContainerBox>:
    orientation: 'horizontal'
    HBoxWidget:
    VBoxWidget1:

我计划以垂直方式拥有多个 VBoxWidget 但它不起作用。

此外,为了实现[9] Label,我想我会有一个具有2行的BoxLayout,在水平方向上,第二行将具有上述属性。但这是行不通的。以下是我得到的。我尝试将 size_hint 设置为 1,.25,即整个区域将分为 4 部分,但它没有给出所需的结果。

PY 文件:

from kivy.app import App
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout

class HBoxWidget(Widget):
    def __init__(self, **kwargs):
        super(HBoxWidget, self).__init__(**kwargs)

class VBoxWidget1(Widget):
    def __init__(self, **kwargs):
        super(VBoxWidget1, self).__init__(**kwargs)


class ContainerBox(BoxLayout):
    def __init__(self, **kwargs):
        super(ContainerBox, self).__init__(**kwargs)

class TestApp(App):
    def build(self):
        return ContainerBox() 

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

最佳答案

这个怎么样:我将 HBOX 和 VBOX 小部件更改为 BoxLayout,并将 Label 和另一个 BoxLayout 添加到 ContainerBox。看起来很像你的图

enter image description here

<HBoxWidget>:

    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        Image:
            source: 'duck.jpg'
<VBoxWidget1>:
    BoxLayout:
        orientation: "horizontal"
        size: [1,.25]
        pos: root.pos
        Label:
            text: "Status : "
            color: [0,84,80,19]
        Label:
            text: "Pending"
            color: [0,84,80,19]
        Widget: # Because of this widget Labels are not in the middle, its not on your drawing tough


<ContainerBox>:
    orientation: 'vertical'
    Label:
        text: 'Label'
        size_hint_y: 0.1
    BoxLayout:
        id: four_horizontals
        orientation: 'horizontal'
        HBoxWidget:
        BoxLayout:
            orientation:'vertical'
            # One solution
            #GridLayout:
            #    cols:1
            #    padding: 100
            #    VBoxWidget1:
            #    VBoxWidget1:
            #    VBoxWidget1:
            #    VBoxWidget1:

            #Second Solution
            BoxLayout:
                #size_hint_y: 0 to 1 - it says how much you reduce height. Now its 1/3 of its parent, because there are 3 boxlayouts. if you set 0.5, it will have 1/3*0.5 and the other 2 boxlayouts takes the height which you took from this one 
            BoxLayout:
                orientation:'vertical'
                VBoxWidget1:
                VBoxWidget1:
                VBoxWidget1:
                VBoxWidget1:
            BoxLayout:

python

from kivy.app import App
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout

class HBoxWidget(BoxLayout):
    def __init__(self, **kwargs):
        super(HBoxWidget, self).__init__(**kwargs)

class VBoxWidget1(BoxLayout):
    def __init__(self, **kwargs):
        super(VBoxWidget1, self).__init__(**kwargs)


class ContainerBox(BoxLayout):
    def __init__(self, **kwargs):
        super(ContainerBox, self).__init__(**kwargs)

class TestApp(App):
    def build(self):
        return ContainerBox() 

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

其他信息:

我管理这 4 个标签的方式并不是真正正确的方式。我会给你提示如何更正确地解决它。 检查https://kivy.org/doc/stable/api-kivy.uix.floatlayout.html#module-kivy.uix.floatlayout

在 BoxLayout 上,小部件会自动组织自身 - 水平或垂直,并且根据属于它们的空间大小进行缩放。 使用 FloatLayout,没有任何限制,因此您可以根据需要放置标签。

一般来说,如果您的分辨率发生变化,最好使用 BoxLayouts 来解决。如果您想要更多自由,请使用 FloatLayout,但您必须自己管理小部件缩放和定位

关于python - 基维 : BoxLayout containing horizontal BoxLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280578/

相关文章:

android - 为 Kivy 应用程序保存登录屏幕用户名和密码

Python Numpy 获取 2 个二维数组之间的差异

jquery - 销毁 jQuery 自动完成功能

android - 安装 Python-For-Android Linux Ubuntu,构建错误

python - Kivy 使用 kivy.clock 更新 matplotlib 图

user-interface - 如何增加触摸用户界面,adobe cq5 中的对话框大小?

python - Tensorflow - 显示和手动修改学习模型的权重并导出以供进一步重新学习

python - sift算法颜色不变吗?

python - 如何使用 yahoo_fin 获取季度损益表

java - 数字或类似LCD的显示类?