Search code examples
pythonpython-3.xscrollviewkivykivy-language

Scroll contents of GridLayout in ScrollView - Kivy


I will say first off I have tried every single example on the web involving kv lang. Not once have I had any success.

The idea is pretty simple: As I swipe up/down/scroll the contents of GridLayout() within ScrollView() are scrolled up or down. The best I have been able to do is have the scroll bar fade into view when running the program. Not able to scroll unfortunately.

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

Binding minimum_heightin the __init__ method of the root class (RelativeLayout):

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

I have followed https://github.com/kivy/kivy/blob/master/examples/widgets/scrollview.py converting it to kv lang - scroll bar visible, unable to scroll. Also tried every example on Google Groups and here related to using kv lang. Still no scroll :\

Compiling using buildozer and running on Android fails for an unknown reason.

I would appreciate any assistance that can be given.. I am completely clueless at this point


Solution

  • Being unable to scroll was due to a misunderstanding of Kivy's touch handlers. Completely unrelated to the code mentioned in my question.

    The key is to have GridLayout be larger than ScrollView, so GridLayout can be panned within ScrollView.

    For those wanting to use ScrollView inside ScreenManager using kvlang only:

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