简体   繁体   中英

Error while Adding buttons dynamically to Screen in ScreenManager Kivy

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.

Traceback (most recent call last): File "main.py", line 174, in screenManager.add_widget( HomeScreen( name = 'homeScreen' ) ) File "main.py", line 162, in init 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'

Here is my main.py

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

kivy file

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

Let's start with __init__ :

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)

although this looks fine and is called when you make an instance, there's one basic thing missing - super() . You need super() to initialize the Screen 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 inherit the behavior.

Note that the Screen itself is a RelativeLayout 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM