在ScrollView中滚动GridLayout的内容 - Kivy

时间:2018-02-05 00:41:24

标签: python python-3.x scrollview kivy kivy-language

首先我会说我已经尝试过涉及kv lang的网络上的每一个例子。我没有取得任何成功。

这个想法很简单:当我向上/向下/向上滑动时,GridLayout()ScrollView()的内容会向上或向下滚动。 我能做的最好的事情是在运行程序时滚动条淡入视图。不幸的是无法滚动。

<Root>
grid_layout: grid_layout
ScreenManager:
...
   Screen:
   ...
        ScrollView:
            GridLayout:
                id: grid_layout
                size_hint_y: None
                cols: 1
                height: self.minimum_height

                <list of buttons>

在根类的minimum_height方法中绑定__init__(RelativeLayout):

grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))

我已关注https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py将其转换为kv lang - 滚动条可见,无法滚动。还尝试了Google网上论坛的每个示例,此处与使用kv lang相关。还是没有卷轴:\

使用buildozer进行编译并在Android上运行失败原因不明。

我很感激可以给予任何帮助..我现在完全无能为力

2 个答案:

答案 0 :(得分:1)

此:

height: self.minimum_height

应该是:

minimum_height: self.height

这是不必要的:

grid_layout = ObjectProperty(None)
self.grid_layout.bind(minimum_height=self.grid_layout.setter('height'))

除非内容大于滚动视图的高度,否则它也不会滚动:

完整代码:

from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

Builder.load_string('''
<Root>:
    ScrollView:
        size_hint: 1, .1
        # setting the width of the scrollbar to 50pixels
        bar_width: 50
        # setting the color of the active bar using rgba
        bar_color: 5, 10, 15, .8
        # setting the color of the inactive bar using rgba
        bar_inactive_color: 5, 20, 10, .5
        # setting the content only to scroll via bar, not content
        scroll_type: ['bars']
        GridLayout:
            size_hint_y: None
            cols: 1
            minimum_height: self.height
            Button
                text: 'one'
            Button:
                text: 'two'
            Button:
                text: 'three'
            Button:
                text: 'four'
''')

class Root(FloatLayout):
    pass

class DemoApp(App):

    def build(self):
        return Root()

if __name__ == '__main__':
    DemoApp().run()

答案 1 :(得分:0)

无法滚动是由于对Kivy的touch处理程序的误解。与我的问题中提到的代码完全无关。

关键是让GridLayout大于ScrollView,因此GridLayout可以在ScrollView内平移。

对于那些只想使用kvlang在ScrollView内使用ScreenManager的人:

ScreenManager:
id: screen_manager

    Screen:
        manager: screen_manager
        id: main_screen
        name: 'main'        

        ScrollView:
            bar_width: 4
            # pos_hint defaults to 1,1 so no need to declare it
            GridLayout:
                size_hint_y: None
                cols: 1 
                # you do not need to manually bind to setter('height') in
                # python - perfectly possible with kv lang
                # this allows for height to update depending on the
                # collective heights of its child widgets
                height: self.minimum_height
                <----- widgets here ----->
                # for scroll to show/work there must be more widgets 
                # then can fit root.height. If not there is no need 
                # for scrollview :)