TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Alex Onel picture Alex Onel · Aug 27, 2019 · Viewed 11.5k times · Source

I am trying to make a form app and I don t understand the error:

TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Code here;

class Myapp(App):
    def build(self):
        return Grid1()

class Grid1(GridLayout):
    def __init__(self,**kwargs):
        super(Grid1,self).__init__(**kwargs)
        self.cols=1

        self.inside=GridLayout()
        self.inside.cols=2

        self.inside.add_widget(Label(text="Your name is :"))
        self.name=TextInput(multiline=False)
        self.inside.add_widget(self.name)


        self.inside.add_widget(Label(text="Your Last name is :"))
        self.lastname=TextInput(multiline=False)
        self.inside.add_widget(self.lastname)


        self.inside.add_widget(Label(text="Your email is :"))
        self.email=TextInput(multiline=False)
        self.inside.add_widget(self.email)

        self.submit=Button(text="Submit",font=40)
        self.add_widget(self.submit)

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

Error

File ".\kivyprima.py", line 38, in <module> Myapp().run()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line 829, in run root = self.build()
File ".\kivyprima.py", line 10, in build return Grid1()
File ".\kivyprima.py", line 34, in init self.submit=Button(text="Submit",font=40)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\behaviors\button.py", line 121, in init

Answer

Esdras Xavier picture Esdras Xavier · Aug 27, 2019

OK, so the error is actually not in your super(Grid1,self).__init__(**kwargs) statement, it's in the creation of the Submit button. You did:

self.submit = Button(text="Submit", font=40)
self.add_widget(self.submit)

But as the docs say, the font size is set by font_size and not font. The code should be:

self.submit = Button(text="Submit", font_size=40)
self.add_widget(self.submit)

This should work just fine.

Edit

Just want to thank @chepner for pointing this out:

Note that the issue, then, is that font, not being recognized by Button (or anyone else), is simply passed on up the chain until it is ultimately passed to object.__init__ (which raises an error instead of simply ignoring unexpected arguments).