Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't pass kwargs to sub class in Kivy — TypeError: object.__init__() takes exactly one argument (the instance to initialize)

I’m very new to programming so forgive me if the solution here is obvious. I’ve done a fair bit of googling and haven’t been able to sort this problem out.
I’m trying to enable a subclass to accept any keyword arguments the base class can accept, as well as a few additional ones I want to pass, but I keep running into the following error: TypeError: object.init() takes exactly one argument (the instance to initialize).

If I comment out the keyword arguments not built into the base class (workout_title, last_completed, and lifts) the code runs no problem.

How do I allow the subclass to take my additional kwargs?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class SelectionBanner(GridLayout):
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)
    
        grid = GridLayout(
            rows = 3, 
            size_hint = (.8,1)
            )
        workout_title_label = Label(
            size_hint = (1,.33), 
            pos_hint = {"top": 1, "left": .5}, 
            text = "workout tile"
            )
        last_completed_label = Label(
            size_hint = (1,.33), 
            pos_hint = {"top": 1, "left": .5}, 
            text = "last_completed"
            )
        lifts_label = Label(
            size_hint = (1,.33), 
            pos_hint = {"top": 1, "left": .5}, 
            text = "lifts"
            )
        button = Button(
            size_hint = (.2, 1),
            text = "Click me!"
        )

        grid.add_widget(workout_title_label)
        grid.add_widget(last_completed_label)
        grid.add_widget(lifts_label)
    
        self.add_widget(grid)
        self.add_widget(button)

def build_selection_page(self):
    data = db.reference("/workout_templates").get()
    selection_page = self.root.ids['home_screen'].ids['top_layout']
    selection_page.add_widget(SelectionBanner(
        cols = 2, 
        padding = 10, 
        spacing = 10, 
        pos_hint = {"top": 1, "left": .5}, 
        size_hint = (1, .2),
        workout_title = "Workout B",
        last_completed = "Yesterday",
        lifts = "Bench and stuff"
        ))



>Solution :

Just add Properties for the new keyword arguments, like this:

class SelectionBanner(GridLayout):
    workout_title = StringProperty('')
    last_completed = StringProperty('')
    lifts = StringProperty('')

    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)
    .
    .
    .
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading