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

2024-09-28 20:50:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想避免使用浮动布局,因为它会花费更多的时间,我会认为应该有一个方法来实现这一点使用框布局和大小提示等

目前,我有6个框布局,每个布局内都有一个网格布局,以适当地定位窗口小部件,然而,在一些网格布局中,我希望比其他一些小部件少一些,这会导致小部件填充剩余空间,而不管我是否添加了大小提示。在

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

下面是迄今为止代码的图像:App Example

最上面的两个框/网格布局中包含的小部件较少,这两个布局与其他框/网格布局的大小相同。在

如能提供任何帮助,我们将不胜感激。在

以下是.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"

Tags: textidsize部件布局rowsm3cols
1条回答
网友
1楼 · 发布于 2024-09-28 20:50:46

一种可能是使用row_force_default: True属性来强制行的高度。要指定高度,请使用row_default_height属性,并将其与某个完整gridlayouts的行大小绑定,例如,使用它的一个小部件。在

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

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

可复制示例:

^{pr2}$

输出:

enter image description here

相关问题 更多 >