繁体   English   中英

我如何在 KIVY 的框布局中保持标签和文本输入小部件的大小相同

[英]How do i keep Label and Text input widget sizes the same inside a box layout in KIVY

我有一个手风琴布局,我试图弄清楚如何在 Box Layouts 中拥有不同数量的小部件,并使它们与其他 Box Layouts 中的其他小部件保持相同的大小。

我想避免使用 Float 布局,因为它会花费更多时间,并且我认为应该有一种方法可以使用框布局和大小提示等来完成此操作。

目前我有 6 个盒子布局,每个盒子内部都有一个网格布局,以适当地放置小部件,但是,在某些网格布局中,我想要的小部件比其他小部件少,这会导致小部件填充剩余空间,无论我是否是否输入尺寸提示。

我试过在没有内容的网格布局中添加标签,但它不会改变尺寸排列。 如果我添加额外的文本输入小部件,它会调整以适应并且与其他框布局相同,但不是我想要的。

这是到目前为止代码的样子的图像: 应用示例

我希望内部小部件较少的前两个框/网格布局与其他框/网格布局大小相同。

任何有关如何实现这一目标的帮助将不胜感激。

以下是 .kv 代码的摘录:(我删掉了一些,但如果太长,我深表歉意)

<CustButton@Button>:
    font_size: 18
    spacing: [10, 10]
    size_hint: [.5, .8]






<CustLabel@Label>:
    font_size: 18
    pos_hint: [None, None]
    color: 1, 0.757, 0.145, 1
    size_hint: [.8,.8]


<CustLabel2@Label>:
    font_size: 18
    pos_hint: [None, None]
    color: 1, 0.757, 0.145, 1
    size_hint: [.8,.8]


<CustTextInput@TextInput>:
    font_size: 18
    write_tab: False    
    size_hint: [.5,.5]
AccordionItem:
            title: "Water Figures"


            GridLayout:
                padding: [10,10]
                rows: 3
                cols: 0
                BoxLayout:
                    orientation: 'horizontal'
                    padding: [10,10]
                    GridLayout:
                        rows: 4
                        cols: 2
                        padding: [10,10]

                        CustLabel:
                            text: "Port FW Tank Volume"


                        CustTextInput:
                            id: pfwtv
                            hint_text: "m3"



                        CustLabel:
                            text: "Stbd FW Tank Volume"


                        CustTextInput:
                            id: sfwtv
                            hint_text: "m3"

                        CustLabel:
                            text: "Fire Fight FW Tank Volume"


                        CustTextInput:
                            id: fffwtv
                            hint_text: "m3"



                    GridLayout:
                        rows: 4
                        cols: 2
                        padding: [10,10]

                        CustLabel:
                            text: "Port DW Tank Volume"


                        CustTextInput:
                            id: pdwtv
                            hint_text: "m3"



                        CustLabel:
                            text: "Stbd DW Tank Volume"


                        CustTextInput:
                            id: sdwtv
                            hint_text: "m3"



                BoxLayout:
                    orientation: 'horizontal'
                    padding: [10,10]
                    GridLayout:
                        rows: 4
                        cols: 2
                        padding: [10,10]
                        CustLabel:
                            text: "Today #1 Evap Meter"


                        CustTextInput:
                            id: ter_1
                            hint_text: "m3"



                        CustLabel:
                            text: "Today #2 Evap Meter"


                        CustTextInput:
                            id: ter_2
                            hint_text: "m3"

                        CustLabel:
                            text: "Previous #1 Evap Meter"


                        CustTextInput:
                            id: per_1
                            hint_text: "m3"



                        CustLabel:
                            text: "Previous #2 Evap Meter"


                        CustTextInput:
                            id: per_2
                            hint_text: "m3"

                    GridLayout:
                        rows: 4
                        cols: 2
                        padding: [10,10]

                        CustLabel2:
                            text: "Today Total FW Volume"


                        CustTextInput:
                            id: ttfwv
                            hint_text: "m3"



                        CustLabel:
                            text: "Previous Total FW Volume"


                        CustTextInput:
                            id: ptfwv
                            hint_text: "m3"

                        CustLabel2:
                            text: "Today Total DW Volume"


                        CustTextInput:
                            id: ttdwv
                            hint_text: "m3"



                        CustLabel:
                            text: "Previous Total DW Volume"


                        CustTextInput:
                            id: ptdwv
                            hint_text: "m3"

                BoxLayout:
                    padding: [10, 10]
                    orientation: 'horizontal'
                    GridLayout:
                        padding: [10,10]
                        rows: 4
                        cols: 2

                        CustLabel:
                            text: "No 1 Total Evap Output"


                        CustTextInput:
                            id: teout_1
                            hint_text: "m3"



                        CustLabel:
                            text: "No 2 Total Evap output"


                        CustTextInput:
                            id: teout_2
                            hint_text: "m3"

                        CustLabel:
                            text: "Date"


                        CustTextInput:
                            hint_text: root.dt1
                            font_size: 25



                        CustLabel:
                            text: "Top Left"


                        CustTextInput:
                            hint_text: root.dt2
                            font_size: 25

                    GridLayout:
                        rows: 4
                        cols: 2
                        padding: [10,10]
                        CustLabel:
                            text: "To be determined"


                        CustTextInput:
                            hint_text: "m3"



                        CustLabel:
                            text: "To be determined"


                        CustTextInput:
                            hint_text: "m3" 

                        CustLabel:
                            text: "To be determined"


                        CustTextInput:
                            hint_text: "m3"



                        CustButton:
                            text: "Calculate"


                        CustTextInput:
                            hint_text: "m3"

一种可能性是使用row_force_default: True属性来强制行的高度。 要指定高度,您可以使用row_default_height属性并将其与完整网格布局之一的行大小绑定,例如,使用其小部件之一。

例如,使用ptdwv高度作为参考:

GridLayout:
    row_force_default: True
    row_default_height: ptdwv.height
    rows: 4
    cols: 2
    padding: [10,10]

可重现的例子:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.accordion import Accordion


kv_text = '''
<CustButton@Button>:
    font_size: 18
    spacing: [10, 10]
    size_hint: [.5, .8]

<CustLabel@Label>:
    font_size: 18
    pos_hint: [None, None]
    color: 1, 0.757, 0.145, 1
    size_hint: [.8,.8]


<CustLabel2@Label>:
    font_size: 18
    pos_hint: [None, None]
    color: 1, 0.757, 0.145, 1
    size_hint: [.8,.8]


<CustTextInput@TextInput>:
    font_size: 18
    write_tab: False    
    size_hint: [.5,.5]

<MyAccordion>:
    orientation: 'horizontal'
    AccordionItem:
        title: "Water Figures"
        GridLayout:
            padding: [10,10]
            rows: 3
            cols: 0
            BoxLayout:
                orientation: 'horizontal'
                padding: [10,10]

                GridLayout:
                    row_force_default: True
                    row_default_height: ptdwv.height
                    rows: 4
                    cols: 2
                    padding: [10,10]

                    CustLabel:
                        text: "Port FW Tank Volume"

                    CustTextInput:
                        id: pfwtv
                        hint_text: "m3"

                    CustLabel:
                        text: "Stbd FW Tank Volume"

                    CustTextInput:
                        id: sfwtv
                        hint_text: "m3"

                    CustLabel:
                        text: "Fire Fight FW Tank Volume"

                    CustTextInput:
                        id: fffwtv
                        hint_text: "m3"

                GridLayout:
                    rows: 4
                    cols: 2
                    row_force_default: True
                    row_default_height: ptdwv.height
                    padding: [10,10]

                    CustLabel:
                        text: "Port DW Tank Volume"

                    CustTextInput:
                        id: pdwtv
                        hint_text: "m3"

                    CustLabel:
                        text: "Stbd DW Tank Volume"

                    CustTextInput:
                        id: sdwtv
                        hint_text: "m3"

            BoxLayout:
                orientation: 'horizontal'
                padding: [10,10]
                GridLayout:
                    rows: 4
                    cols: 2
                    padding: [10,10]
                    CustLabel:
                        text: "Today #1 Evap Meter"

                    CustTextInput:
                        id: ter_1
                        hint_text: "m3"

                    CustLabel:
                        text: "Today #2 Evap Meter"

                    CustTextInput:
                        id: ter_2
                        hint_text: "m3"

                    CustLabel:
                        text: "Previous #1 Evap Meter"

                    CustTextInput:
                        id: per_1
                        hint_text: "m3"

                    CustLabel:
                        text: "Previous #2 Evap Meter"

                    CustTextInput:
                        id: per_2
                        hint_text: "m3"

                GridLayout:
                    rows: 4
                    cols: 2
                    padding: [10,10]

                    CustLabel2:
                        text: "Today Total FW Volume"

                    CustTextInput:
                        id: ttfwv
                        hint_text: "m3"

                    CustLabel:
                        text: "Previous Total FW Volume"

                    CustTextInput:
                        id: ptfwv
                        hint_text: "m3"

                    CustLabel2:
                        text: "Today Total DW Volume"

                    CustTextInput:
                        id: ttdwv
                        hint_text: "m3"

                    CustLabel:
                        text: "Previous Total DW Volume"


                    CustTextInput:
                        id: ptdwv
                        hint_text: "m3"

            BoxLayout:
                padding: [10, 10]
                orientation: 'horizontal'
                GridLayout:
                    padding: [10,10]
                    rows: 4
                    cols: 2

                    CustLabel:
                        text: "No 1 Total Evap Output"

                    CustTextInput:
                        id: teout_1
                        hint_text: "m3"

                    CustLabel:
                        text: "No 2 Total Evap output"

                    CustTextInput:
                        id: teout_2
                        hint_text: "m3"

                    CustLabel:
                        text: "Date"

                    CustTextInput:
                        hint_text: '11/07/2017'#root.dt1
                        font_size: 25

                    CustLabel:
                        text: "Top Left"

                    CustTextInput:
                        hint_text: '20:00'#root.dt2
                        font_size: 25

                GridLayout:
                    rows: 4
                    cols: 2
                    padding: [10,10]
                    CustLabel:
                        text: "To be determined"

                    CustTextInput:
                        hint_text: "m3"

                    CustLabel:
                        text: "To be determined"

                    CustTextInput:
                        hint_text: "m3" 

                    CustLabel:
                        text: "To be determined"

                    CustTextInput:
                        hint_text: "m3"

                    CustButton:
                        text: "Calculate"

                    CustTextInput:
                        hint_text: "m3"
'''
class MyAccordion(Accordion):
    pass
class MyApp(App):
    def build(self):
        return MyAccordion()

def main():
    Builder.load_string(kv_text)
    app = MyApp()
    app.run()

if __name__ == '__main__':
    main()

输出:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM