我如何在 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 个盒子布局,每个盒子内部都有一个网格布局,以适当地放置小部件,但是,在某些网格布局中,我想要的小部件比其他小部件少,这会导致小部件填充剩余部分space 不管我是否输入尺寸提示。
我试过在没有内容的网格布局中添加标签,但它不会改变尺寸排列。如果我添加额外的文本输入小部件,它会根据需要进行调整,并且与其他框布局相同,但不是我想要的。
这是到目前为止代码的样子的图片:
前两个 box/Grid 内部小部件较少的布局是我想要与其他 Box/Grid 布局相同大小的布局。
任何有关如何实现此目标的帮助将不胜感激。
下面是 .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()
输出:
我有一个手风琴布局,我正在尝试弄清楚如何在 Box Layouts 中拥有不同数量的小部件,并使它们与其他 Box Layouts 中的其他小部件保持相同的大小。
我想避免使用 Float 布局,因为它会花费更多时间,我认为应该有一种方法可以使用框布局和大小提示等来完成此操作。
目前我有 6 个盒子布局,每个盒子内部都有一个网格布局,以适当地放置小部件,但是,在某些网格布局中,我想要的小部件比其他小部件少,这会导致小部件填充剩余部分space 不管我是否输入尺寸提示。
我试过在没有内容的网格布局中添加标签,但它不会改变尺寸排列。如果我添加额外的文本输入小部件,它会根据需要进行调整,并且与其他框布局相同,但不是我想要的。
这是到目前为止代码的样子的图片:
前两个 box/Grid 内部小部件较少的布局是我想要与其他 Box/Grid 布局相同大小的布局。
任何有关如何实现此目标的帮助将不胜感激。
下面是 .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()
输出: