Getting a too much iteration done before the next frame error Kivy

Viewed 18
class StackLayoutExample(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for i in range(0, 50):
            b = Button(text=str(i + 1), size_hint=(0.2, 0.2))

            self.add_widget(b)


<ScrollViewExample@ScrollView>:
    StackLayoutExample:
        size_hint:1,None
        height: self.minimum_height

Basically I want to make a scroll bar made up of the stack of 100 buttons whoch I looped through

1 Answers

The problem is your use of size_hint. Keep in mind that setting the size_hint for a widget means that it changes its size when its parent size changes. But, in your case, that parent is the StackLayoutExample, and you have it set to do height: self.minimum_height. So, what happens is that every time you add a Button to the StackLayoutExample, the size of the StackLayoutExample changes, which triggers a change in the size of each Button which triggers a new calculation of self.minimum_height for the StackLayoutExample, changing its size, which again triggers changes in the Buttons (due to size_hint).

The fix is to eliminate the part of the size_hint that affects the minimum_height of the StackLayoutExample. Just set an explicit height for those Buttons to avoid that recursion:

class StackLayoutExample(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for i in range(0, 50):
            b = Button(text=str(i + 1), size_hint=(0.2, None), height=50)  # set an explicit height
            self.add_widget(b)
Related