TypeError: object.__init__() takes exactly one argument (the instance to initialize)(TypeError: object.__init__() 只接受一个参数(要初始化的实例))
问题描述
我正在尝试制作一个表单应用程序,但我不明白错误:
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).
这篇关于TypeError: object.__init__() 只接受一个参数(要初始化的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError: object.__init__() 只接受一个参数(要初始化的实例)
基础教程推荐
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
