获得“TypeError:object .__ init()__不带参数

时间:2017-10-08 19:12:54

标签: python python-3.x kivy kivy-language

我正在尝试实现我用kivy编写的游戏的图形部分。由于我是kivy的新手,我通过了ts文档,我找到了一些我研究和使用过的编程示例。在其中一个样本中,我得到:

TypeError: object.__init__() takes no parameters

以下是代码:

from kivy.app import App;
from kivy.uix.label import Label;
from kivy.uix.gridlayout import GridLayout;
from kivy.uix.textinput import TextInput;

class LoginScreen(GridLayout):
    def __init__(self, **kwargs):

        #super(LoginScreen, self).__new__(**kwargs) # == super(LoginScreen, self).__init__(**kwagrs) 
        #GridLayout.__init__()
        super().__init__(**kwargs);
        self.cols = 2 # The colors

        # Creating the Object for username and then adding it into Canvans 
        self.add_widget(Label(text="Username: "))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)

        # Creating the Object for password and then adding it into Canvans
        self.add_widget(None,Label(Text="password:"))
        self.password = TextInput(password=True,multiline=False)
        self.add_widget(self.password)

class SimpleKivy(App):
    def build(self):
        return LoginScreen();

if __name__ == "__main__":
    SimpleKivy().run();

1 个答案:

答案 0 :(得分:2)

错误在这一行:

self.add_widget(None,Label(Text="password:"))

您无需使用None,并将Text=更改为text=,因为kivy的关键字args全部为小写。所以改成它:

self.add_widget(Label(text="password:"))

另外,看看Kv language它对于使用kivy构建应用程序非常有用。