2017-07-11 42 views
2

我有一個手風琴佈局,我想弄清楚如何在盒子佈局中有不同數量的小部件,並保持它們的大小與其他Box Layouts中的其他小部件。我如何保持標籤和文本輸入小部件大小在KIVY中的盒子佈局內相同

我想避免使用浮動佈局開始有點耗費更多的時間,並會認爲應該做到這一點用箱的佈局和大小提示等方法

目前我有6個箱佈局一個網格佈局裏面的每一個來適當地放置窗口小部件,然而,在一些網格佈局中,我會比其他窗口小部件更少,這會導致窗口小部件填充剩餘的空間,無論我是否放入大小提示。

我曾嘗試在沒有內容的網格佈局中添加標籤,但它不會更改尺寸安排。如果我添加額外的文本輸入小部件,它將調整爲適合並與其他Box Layouts相同,但不是我想要的。

這裏的代碼是什麼樣子至今的圖像:App Example

前兩名盒/網格用更少的部件佈局裏面是我想要的大小與其他盒/網格佈局相同的人。

任何幫助如何實現這一點,將不勝感激。

下面是.kv代碼的摘錄:(我切一些了,但道歉,如果太長)

<[email protected]>: 
    font_size: 18 
    spacing: [10, 10] 
    size_hint: [.5, .8] 






<[email protected]>: 
    font_size: 18 
    pos_hint: [None, None] 
    color: 1, 0.757, 0.145, 1 
    size_hint: [.8,.8] 


<[email protected]>: 
    font_size: 18 
    pos_hint: [None, None] 
    color: 1, 0.757, 0.145, 1 
    size_hint: [.8,.8] 


<[email protected]>: 
    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" 

回答

2

一種可能性是使用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 = ''' 
<[email protected]>: 
    font_size: 18 
    spacing: [10, 10] 
    size_hint: [.5, .8] 

<[email protected]>: 
    font_size: 18 
    pos_hint: [None, None] 
    color: 1, 0.757, 0.145, 1 
    size_hint: [.8,.8] 


<[email protected]>: 
    font_size: 18 
    pos_hint: [None, None] 
    color: 1, 0.757, 0.145, 1 
    size_hint: [.8,.8] 


<[email protected]>: 
    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() 

輸出:

enter image description here

+0

很好,再次感謝您的協助。它正是我想要的。 – Aiden

相關問題