【发布时间】:2016-02-29 15:11:05
【问题描述】:
在我正在创建的应用程序中,我有一个包含几个屏幕实例的屏幕管理器。在其中一个中,我希望有 2 个标准小部件和一些由用户动态添加的按钮。如果这些按钮太多,用户应该能够滚动它们直到找到他想要的那个。
为了实现这一点,我使用了一个包含两个对象的网格布局:另一个网格布局和一个滚动视图。网格布局负责 2 个标准小部件(文本输入和按钮)。滚动视图负责动态添加的按钮。
我希望滚动视图部分占据窗口的较大部分(比如 75%),以便用户可以更清楚地看到按钮,并且带有 2 个标准小部件的网格布局应该占据剩余部分。然而,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)
【问题讨论】: