Python Kivy ScrollView not Scrolling with StackLayout

Question:

I was trying to add a ScrollView to my code along with a StackLayout but it is not working.

It should have been enabling the user to scroll but it just shows the upper part/the part that fits into the screen and refuses to scroll further.

Here is the code:

class ScrollViewExample(ScrollView):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.do_scroll_x = False
        self.do_scroll_y = True
        self.add_widget(StackLayoutExample())

class StackLayoutExample(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = "lr-tb"
        #this loop adds a hundred buttons to the layout, there isnt a problem with this part
        for i in range(100):
            self.add_widget(Button(text=str(i+1), size_hint=[None, None], size=["100dp", "100dp"]))
        self.size_hint_y = None
        self.height = self.minimum_height
Asked By: maydolina

||

Answers:

Let’s keep this as simple as possible.

When you set height in __init__ to its minimum_height, it sets the initial available height. But when the actual height changes (depending on window or parent), it does nothing. In order to listen to this changes you need to track the changes with a event listener i.e. a callback method/function.

Now this can be done in kivy in various ways. One of the easiest might be using default method setter as follows,

self.bind(minimum_height=self.setter('height'))

Which translates to set the height to its minimum_height whenever it changes.

Answered By: ApuCoder
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.