Search code examples
pythonkivy

AttributeError: 'super' object has no attribute '__getattr__' in Kivy


I am facing an error when trying to add buttons to a specific GridLayout using ids. Ideally the code below should generate 10 buttons in the GridLayout with an id of grids, but instead the error that shows up is AttributeError: 'super' object has no attribute 'getattr'

The code in my main.py file is -->

    import kivy
from kivy.app import App 
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.clock import mainthread

Builder.load_file("design.kv")

class RoundedButton(Button):
    pass
    
class RootWidget(ScreenManager):
    pass


class MainScreen(Screen):

    def on_enter(self):
        for i in range(10):
            button = RoundedButton()
            self.ids.grids.add_widget(button)


class MainApp(App):
    def build(self):
        return RootWidget()

class RootWidget(ScreenManager):
    pass



if __name__ == "__main__":
    MainApp().run()

and the code in my design.kv file is -->

<MainScreen>:
    canvas.before:
        Color:
            rgba: (245/255,245/255,245/255,245/255)
        Rectangle:
            pos: self.pos
            size: self.size
    
    GridLayout:
        id: box1
        cols:1
        spacing:5

        GridLayout:
            id: box
            cols:1
            size_hint_y: 0.10

            TextInput: 
                id: ti
                hint_text: 'Search'
                size_hint: 1, 0.05
                text_size: self.width, self.height
                background_normal: ''

        ScrollView:
            id: scrolls
            do_scroll_x:False
            spacing: 10, 5

            GridLayout:
                id: grids
                cols:1
                
                RoundedButton:
                    text: "hi"
                    size_hint: .98, .25
                
                
                    
                
<RoundedButton@Button>
    background_color: (0,0,0,0)
    background_normal: ''
    canvas.before:
        Color:
            rgba: (1,0,0,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [10]


<RootWidget>:
    MainScreen:
        name: "main_screen"

I was wondering what the issue is


Solution

  • You need to call another function with Clock.schedule_once in on_enter function and try to load your widgets in it. It happens because kivy doesn't let you add widgets before first frame so we can give order kivy to add widgets soon as possible with that.

    Example code below:

    def on_enter(self):
        Clock.schedule_once(self.load_buttons)
    def load_buttons(self,*args):
        for i in range(10):
            button = RoundedButton()
            self.ids.grids.add_widget(button)