24

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
markwalker_
  • 12,078
  • 7
  • 62
  • 99
Alex Onel
  • 373
  • 1
  • 2
  • 9
  • `return Grid1()` here keyword arguments should be passed? – Olvin Roght Aug 27 '19 at 10:40
  • 1
    @OlvinRoght No, `**kwargs` is totally optional – DeepSpace Aug 27 '19 at 10:40
  • @AlexOnel provide the full stacktrace – DeepSpace Aug 27 '19 at 10:41
  • @DeepSpace, yes, I forgot to put question mark at the end of question. – Olvin Roght Aug 27 '19 at 10:42
  • File ".\kivyprima.py", line 38, in 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__ – Alex Onel Aug 27 '19 at 10:53
  • super(ButtonBehavior, self).__init__(**kwargs) File "C:\Users\Alex\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\label.py", line 318, in __init__ super(Label, self).__init__(**kwargs) File "C:\Users\Alex\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 350, in __init__ super(Widget, self).__init__(**kwargs) File "kivy\_event.pyx", line 243, in kivy._event.EventDispatcher.__init__ TypeError: object.__init__() takes exactly one argument (the instance to initialize)) – Alex Onel Aug 27 '19 at 10:53
  • Add the full traceback to the question; it's virtually unreadable as a comment. – chepner Aug 27 '19 at 13:24
  • Please update the title, it is misleading. – Victor Zuanazzi Dec 06 '21 at 10:33

1 Answers1

16

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

martineau
  • 119,623
  • 25
  • 170
  • 301
Esdras Xavier
  • 867
  • 1
  • 7
  • 18
  • 4
    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). – chepner Aug 27 '19 at 13:46