TypeError: object.__init__() 只接受一个参数(要初始化的实例)

2022-01-15 00:00:00 python kivy

问题描述

我正在尝试制作一个表单应用程序,但我不明白错误:

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)

代码在这里;

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

错误

File ".kivyprima.py", line 38, in <module> Myapp().run()
File "C:UsersAlexAppDataLocalProgramsPythonPython37libsite-packageskivyapp.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:UsersAlexAppDataLocalProgramsPythonPython37libsite-packageskivyuixehaviorsutton.py", line 121, in init


解决方案

好的,所以错误实际上是 not 在你的 super(Grid1,self).__init__(**kwargs) 语句,它在 Submit 按钮的创建中.你做到了:

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)

但正如 docs 所说,字体大小是由 font_size 而不是 font 设置.代码应该是:

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)

这应该可以正常工作.

只想感谢@chepner 指出这一点:

Just want to thank @chepner for pointing this out:

请注意,问题在于该字体无法被按钮(或其他任何人),只是简单地向上传递,直到它最终传递给 object.__init__ (这会引发错误简单地忽略意想不到的论点).

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

相关文章