Error while Adding buttons dynamically to Screen in ScreenManager Kivy
<p>I am trying to add buttons dynamicaly to screen. I have the following error when I run the app. Please help me resolve the issue.</p>

<blockquote>
  <p>Traceback (most recent call last):    File "main.py", line 174, in
  
       screenManager.add_widget( HomeScreen( name = 'homeScreen' ) )    File "main.py", line 162, in <strong>init</strong>
       self.add_widget(btn)    File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/uix/floatlayout.py",
  line 111, in add_widget
       pos=self._trigger_layout,  AttributeError: 'HomeScreen' object has no attribute '_trigger_layout'</p>
</blockquote>

<p>Here is my main.py</p>

<pre><code>class HomeScreen(Screen):

    def __init__(self, **kwargs):
        for i in range(80):
            btn = Button(text=str(i), size=(90, 90), size_hint=(None, None))
            self.add_widget(btn)


# Screen Manager
screenManager = ScreenManager( transition = FadeTransition() )

# Add all screens to screen manager
#screenManager.add_widget( UsernameScreen( name = 'usernameScreen' ) )
#screenManager.add_widget( PasswordScreen( name = 'passwordScreen' ) )
#screenManager.add_widget( LevelTwoScreen( name = 'levelTwoScreen' ) )
#screenManager.add_widget( LevelTwoScreen( name = 'levelThreeScreen' ) )

screenManager.add_widget( HomeScreen( name = 'homeScreen' ) )

class ThreeLevelAuthApp(App):
    def build(self):
        return screenManager

if __name__ == '__main__':
    ThreeLevelAuthApp().run()
</code></pre>

<p>kivy file</p>

<pre><code>&lt;HomeScreen&gt;:
    ScrollView:
        size_hint: None, None
        size: 400, 400
        pos_hint: { 'center_x': 0.5,'center_y': 0.5 }   
        do_scroll_x: False

    GridLayout:
        cols: 6
        padding: 20
        spacing: 20
        size_hint: None, None
        width: 400
</code></pre>

<p>Let's start with <code>__init__</code>:</p>

<pre><code>class HomeScreen(Screen):

    def __init__(self, **kwargs):
        for i in range(80):
            btn = Button(text=str(i), size=(90, 90), size_hint=(None, None))
            self.add_widget(btn)
</code></pre>

<p>although this looks fine and is called when you make an instance, there's one basic thing missing - <a class='doc-link' href="http://stackoverflow.com/documentation/python/419/classes/1399/basic-inheritance#t=201610142247298133988"><code>super()</code></a>. You need <code>super()</code> to initialize the <code>Screen</code> first, so that it has all the required variables and methods that make it a class with real behavior and therefore makes you able to actually <em>inherit</em> the behavior.</p>

<p>Note that the <code>Screen</code> itself is a <a href="https://kivy.org/docs/api-kivy.uix.relativelayout.html" rel="nofollow"><code>RelativeLayout</code></a> and you'll need to handle additional stuff such as positioning and/or sizing if you won't use a layout that does that for you.</p>

<pre><code>import random
class HomeScreen(Screen):

    def __init__(self, **kwargs):
        super(HomeScreen, self).__init__(**kwargs)
        for i in range(80):
            btn = Button(text=str(i), size=(90, 90),
                         size_hint=(None, None),
                         pos=[random.randint(0,500), random.randint(0,500)])
            self.add_widget(btn)
</code></pre>

python kivy