2016-02-29 88 views
0

在我創建的應用程序中,我有一個屏幕管理器,它擁有幾個屏幕實例。其中一個我想要有2個標準的小部件,以及用戶動態添加的一些按鈕。如果這些按鈕太多,用戶應該能夠滾動它們直到找到他想要的。無法正確設置窗口小部件的尺寸

爲了達到這個目的,我使用了一個網格佈局來保存兩個對象:另一個網格佈局和一個滾動視圖。網格佈局負責2個標準小部件(文本輸入和按鈕)。滾動視圖負責動態添加的按鈕。

我希望scrollview部分佔據窗口的較大部分(比如75%),這樣用戶可以更清楚地看到按鈕,並且具有2個標準小部件的網格佈局應占據剩餘部分。但是,網格佈局最終會佔據自身窗口的大部分。

下面是一段代碼(假設動態添加的按鈕,現在是15):

sm = ScreenManager() 

class scr1(Screen): 
    pass 

#layout that occupies the entire window. Everything else will be added on this 
glayout = GridLayout(cols=1) 

#layout that should occupy 25% of the window 
layout1 = GridLayout(cols=1,size_hint_y=0.25) 

#layout to be added on the scrollview 
layout2 = GridLayout(cols=1,size_hint_y=None) 
layout2.bind(minimum_height=layout2.setter('height')) 

#screen to hold the glayout 
screen1 = scr1(name = '1') 

#adding a couple of widgets to layout1 
layout1.add_widget(Button(text = 'hey')) 
layout1.add_widget(TextInput(text = 'hey')) 

#scroller that should occupy 75% of the window 
scroller = ScrollView(size_hint = (1,None)) 
scroller.add_widget(layout2) 

#adding buttons to be scrolled 
for i in range(15): 
    layout2.add_widget(Button(text=str(i),size_hint_y=None)) 

#adding scroller and layout1 to glayout and then the glayout to the screen 
glayout.add_widget(scroller) 
glayout.add_widget(layout1) 
screen1.add_widget(glayout) 

#adding the screen to the screen manager 
sm.add_widget(screen1)  

我不是很熟悉kivy使用的定位系統,我不知道我應該如何去解決這個問題。這是程序運行時的樣子。 You can see that the first small part is the scrollview, and the rest is the gridlayout

回答

0

您將ScrollView的size_hint_y設置爲None

from kivy.uix.screenmanager import Screen, ScreenManager 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.scrollview import ScrollView 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
sm = ScreenManager() 

class scr1(Screen): 
    pass 

#layout that occupies the entire window. Everything else will be added on this 
glayout = GridLayout(cols=1) 

#layout that should occupy 25% of the window 
layout1 = GridLayout(cols=1,size_hint_y=0.25) 

#layout to be added on the scrollview 
layout2 = GridLayout(cols=1,size_hint_y=None) 
layout2.bind(minimum_height=layout2.setter('height')) 

#screen to hold the glayout 
screen1 = scr1(name = '1') 

#adding a couple of widgets to layout1 
layout1.add_widget(Button(text = 'hey')) 
layout1.add_widget(TextInput(text = 'hey')) 

#scroller that should occupy 75% of the window 
scroller = ScrollView(size_hint_y=.75) 
scroller.add_widget(layout2) 

#adding buttons to be scrolled 
for i in range(15): 
    layout2.add_widget(Button(text=str(i),height=70,size_hint_y=None)) 

#adding scroller and layout1 to glayout and then the glayout to the screen 
glayout.add_widget(scroller) 
glayout.add_widget(layout1) 
screen1.add_widget(glayout) 

#adding the screen to the screen manager 
sm.add_widget(screen1) 
from kivy.app import App 

class Ap(App): 
    def build(self): 
     return sm 

Ap().run()