滚动视图Kivy中GridLayout的滚动内容

2024-03-28 11:04:19 发布

您现在位置:Python中文网/ 问答频道 /正文

首先我要说的是,我已经在网上尝试过每一个涉及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>

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

^{pr2}$

我已经跟随https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py将其转换为kv语言-滚动条可见,无法滚动。也尝试了谷歌群组上的每一个例子,这里与使用kv语言有关。仍然没有滚动:\

使用buildozer编译并在Android上运行由于未知原因而失败。在

如果您能提供任何帮助,我将不胜感激。。在这一点上我完全不知道


Tags: 程序视图语言内容langroot例子grid
2条回答

这个:

height: self.minimum_height

应该是:

^{pr2}$

这是不必要的:

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()

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

关键是要使GridLayout大于ScrollView,因此{}可以在ScrollView内平移。在

对于那些希望在ScreenManager内使用ScrollView的用户,请仅使用kvlang:

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 :)

相关问题 更多 >