gpt4 book ai didi

python - TypeError : object. __init__() 仅采用一个参数(要初始化的实例)

转载 作者:行者123 更新时间:2023-12-02 09:33:41 43 4
gpt4 key购买 nike

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

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:\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

最佳答案

好的,所以错误实际上不是出现在您的 super(Grid1,self).__init__(**kwargs) 语句中,而是出现在 提交按钮。你做了:

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

但是作为 docs例如,字体大小由 font_size 设置,而不是 font。代码应该是:

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

这应该可以正常工作。

编辑

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

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

关于python - TypeError : object. __init__() 仅采用一个参数(要初始化的实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57672872/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com