简体   繁体   中英

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

I have an accordion layout and I am trying to figure out how to have varying numbers of widgets inside Box Layouts and keep them the same size as the other widgets in other Box Layouts.

I want to avoid using a Float layout as it gets a bit more time consuming and would think there should be a method to accomplish this using box layouts and size hints etc.

Currently I have 6 Box Layouts with a Grid Layout inside each of them to position the widgets appropriately, however, in some of the grid layouts i would like less widgets than some others and this causes the widgets to fill the remaining space regardless of whether i put a size hint in or not.

I have tried adding Labels in the Grid Layouts with no content but it does not change the sizing arrangement. If I add the extra Text Input widgets it adjust to suit and is the same as the other Box Layouts but is not what i want.

Here is an image of what the code looks like so far: 应用示例

The top two box/Grid layouts with less widgets inside are the ones i want the same size as the other Box/Grid Layouts.

Any help on how to achieve this would be appreciated.

Below is an extract of the.kv code: (I cut some out but apologies if it is too long)

<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"

One possibility is to use the row_force_default: True property to force the height of the rows. To specify the height you use the row_default_height property and binds it with the size of the rows of one of your complete gridlayouts, for example, using one of its widgets.

For example, using the ptdwv height as a reference:

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

Reproducible example:

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()

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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