无法正确调整小部件的大小

时间:2016-02-29 15:11:06

标签: python kivy

在我创建的应用程序中,我有一个屏幕管理器,其中包含一些屏幕实例。在其中一个中,我想要有两个标准小部件,以及一些由用户动态添加的按钮。如果这些按钮太多,用户应该能够滚动它们直到找到他想要的按钮。

为了实现这一目标,我使用了一个包含两个对象的网格布局:另一个网格布局和一个滚动视图。网格布局负责2个标准小部件(文本输入和按钮)。 scrollview负责动态添加的按钮。

我希望scrollview部分占据窗口的较大部分(例如75%),这样用户可以更清楚地看到按钮,并且带有2个标准窗口小部件的gridlayout应该占用剩余部分。然而,gridlayout最终占据了窗口的大部分。

这是一段代码(假设现在动态添加的按钮是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

1 个答案:

答案 0 :(得分: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()