ScrollView中的Kivy StackLayout

时间:2017-04-21 15:29:14

标签: python scrollview kivy

我无法让它滚动....

继承我的代码:

    sview=uix.scrollview.ScrollView(size_hint=[1,2],scroll_type=['bars','content'],valign='top',bar_width="10dp")
    bl=uix.stacklayout.StackLayout(size_hint_y=None)
    for x in list:
        bl.add_widget(uix.button.Button(text=x['text'])

    sview.add_widget(bl)

它有点复杂,但这就是它的要点

1 个答案:

答案 0 :(得分:1)

这是scrollview的一个工作示例。来自文档:ScrollView
我编辑它以使用stacklayout。

在ScrollView中尝试更改size_hint,如下所示:
size_hint=(1, None)

还设置ScrollView的大小,使其具有窗口高度:
size=(Window.width, Window.height)

并且如示例中所示,绑定布局的最小高度。在您的案例中添加bl.bind(minimum_height=bl.setter('height'))

最后将按钮的size_hint设置为None:
size_hint_y=None

from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.app import runTouchApp

layout = StackLayout(size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(100):
    btn = Button(text=str(i), size_hint_y=None, height=40)
    layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)

runTouchApp(root)

如果您想更改窗口大小,请尝试使用kv lang。这样它会自动绑定到Windows大小 像这样:

from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.app import App


KV = '''

#: import Window kivy.core.window.Window

ScrollView:
    size_hint: (1,None)
    size: (Window.width, Window.height)
    MyLayout:

'''


class MyLayout(StackLayout):

    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.size_hint_y=(None)
        self.bind(minimum_height=self.setter('height'))
        for i in range(100):
            btn = Button(text=str(i), size_hint_y=None, height=40)
            self.add_widget(btn)


class MyApp(App):

    def build(self):    
        return Builder.load_string(KV)


MyApp().run()