簡體   English   中英

KIVY : BoxLayout 包含水平 BoxLayout

[英]KIVY : BoxLayout containing horizontal BoxLayout

我正在嘗試以垂直方式將 4 個Horizontal框布局堆疊在 BoxLayout 中。

草圖

我的 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 和另一個 BoxLayouts 添加到 ContainerBox。 它看起來很像你的畫

在此處輸入圖片說明

<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,但你必須自己管理小部件的縮放和定位

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM