【问题标题】:Scroll contents of GridLayout in ScrollView - Kivy在 ScrollView 中滚动 GridLayout 的内容 - Kivy
【发布时间】:2018-02-05 00:41:24
【问题描述】:

首先我要说的是,我已经尝试了网络上涉及 kv lang 的每一个示例。我一次也没有成功。

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

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

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

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 Groups 上的每个示例,此处与使用 kv lang 相关。仍然没有滚动:\

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

如果能提供任何帮助,我将不胜感激。此时我完全一无所知

【问题讨论】:

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


    【解决方案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()
    

    【讨论】:

    • 我编辑了我的答案,包括确保滚动条显示、如何根据活动/非活动更改颜色以及设置滚动类型(栏与内容)的示例。
    • 滚动条现在使用.1pos_hint_y 可见。它在活动时短暂闪烁,然后逐渐变为非活动。我无法使用barscontent 滚动。使用pos_hint: 1, .1 将滚动视图指定为屏幕空间的1/10,强制所有GridLayout 内容位于屏幕底部及下方。 :(
    • 这可能与ScrollView()Screen 的孩子有关吗?在我的情况下,它不是布局的直接子代,而是小部件的子代,也就是 ScreenScreenManager()
    • .1 仅作为示例,请根据您自己的项目进行相应调整。您能否展示一下您所拥有的照片,以及您想要帮助我更好地理解的照片。至于屏幕的东西,不太可能,你用对了。
    • 奇怪的是,将pos_hint_y 更改为.1 以上的任何内容都会删除滚动条。界面的其余部分向上移动,但滚动条消失了..
    【解决方案2】:

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

    关键是让GridLayout 大于ScrollView,所以GridLayout 可以在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 :)
    

    【讨论】:

      猜你喜欢
      • 2014-12-28
      • 1970-01-01
      • 2021-12-08
      • 2021-01-29
      • 1970-01-01
      • 2015-12-03
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多