简体   繁体   中英

Error - __init__() takes exactly 2 arguments (1 given)

I'm trying to initialize the class (extraropt) from another .py but it gives me an error, I've searched but I haven't found a solution.

Heres the code of the one py I'm calling from:

main.py:

class GameWindow(ui.ScriptWindow):
    def __init__(self, stream):
        import extraop

        exec 'extraop.extraropt().Show(stream)'

And here's the code of the one py I'm trying to call(init and del only):

extraop.py

class extraropt(ui.Window):
    def __init__(self, stream):
        ui.Window.__init__(self)
        self.BuildWindow()
        self.stream=stream
    def __del__(self):
        ui.Window.__del__(self)

It gives this error:

Error - __init__() takes exactly 2 arguments (1 given)

In the line

exec 'extraop.extraropt().Show(stream)'

You are implicitly calling extraropt.__init__() by creating a new instance of extraopt . In your code, you show that extraropt.__init__() takes a second ( stream ) argument, so you have to pass that in.

extraop.extraropt(stream).Show()

Incidentally, there is no reason why you should be doing an exec rather than explicitly calling it as I did above. There is also no reason for you to have a __del__() method defined as you only call the parent __del__() method anyway.

您需要以这种方式初始化父级:

super(extraropt, self).__init__(stream)

exec 'extraop.extraropt().Show(stream)'stream变量应该传递给extraropt类的构造函数,如下所示:

exec 'extraop.extraropt(stream).Show()'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM