2016-02-29 105 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()