简体   繁体   English

为什么使用 __init__(self, **kwargs) 会返回以下错误代码:SyntaxError: invalid syntax?

[英]Why does using __init__(self, **kwargs) return the following error code: SyntaxError: invalid syntax?

I try to run the code of my Kivy app, but things go wrong when I'm creating a new class.我尝试运行我的 Kivy 应用程序的代码,但是当我创建一个新类时出现问题。 This is the code for it:这是它的代码:

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 2

        self.add_widget(Label(text="First Name: "))
        self.name = TextInput(multiline=False)
        self.add_widget(self.name)

The code doesn't work because of an error in the second line with def __init__(self, **kwargs) .由于第二行 def __init__(self, **kwargs)中的错误,代码不起作用。 The code returns:代码返回:

 SyntaxError: invalid syntax.

I don't know what's wrong with the syntax.我不知道语法有什么问题。 What could be going wrong?可能出什么问题了?

For anyone coming from Tech with Tim's Video, being confused as to why this happens, there is a Typo beneath Class MyGrid(GridLayout):对于任何来自 Tech 和 Tim's Video 的人来说,对于为什么会发生这种情况感到困惑,在Class MyGrid(GridLayout):下面有一个错字Class MyGrid(GridLayout):

There needs to be a space between def and __init__ . def__init__之间需要有一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python的__init__中使用* args和** kwargs的SyntaxError - SyntaxError using *args and **kwargs in an __init__ in Python 为什么 super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined - why does super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined 在 __init__ 中使用 **kwargs - using **kwargs in __init__ 为什么 Visual Studio Code 在派生类中插入“return super().__init__(self)”? - Why does Visual Studio Code insert "return super().__init__(self)" into the derived class? SyntaxError:语法无效为什么会发生这种错误? - SyntaxError: invalid syntax Why does such an error happen? SyntaxError:AOSP 代码中 repo init 的语法无效 - SyntaxError: invalid syntax to repo init in the AOSP code Python anaconda def __init__(self, pad_result: tuple = None, width=0, height=0) 语法错误:无效语法 - Python Syntax error on anaconda def __init__(self, pad_result: tuple = None, width=0, height=0): invalid syntax 这在 python super().__init__(**kwargs) 中有什么作用 - What does this to in python super().__init__(**kwargs) def __init __(self,* args,** kwargs)在python中初始化类 - def __init__(self, *args, **kwargs) initialization of class in python 在__init__中打开** kwargs - Unpacking **kwargs in __init__
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM