TypeError: __init__()正好取2个参数(3给出)

时间:2021-09-11 05:10:49

I don't know why the error shows that it have 3 arguments. Anyone can help?

我不知道为什么误差会显示它有3个参数。任何人都可以帮忙吗?

Traceback:
  line 23, in __init__
    frame = F(self, container)
TypeError: __init__() takes exactly 2 arguments (3 given)

Code:

代码:

class CGPACalculator(Tkinter.Tk):
    def __init__(self, *args, **kwargs):
        Tkinter.Tk.__init__(self, *args, **kwargs)
        container = Tkinter.Frame(self)

        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (Page1, Page2):
            frame = F(self, container)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Page1)

2 个解决方案

#1


1  

@fhdrsdg has pointed out the answer where all the class added should have same definition like this :

@fhdrsdg已经指出了所有添加的类都应该具有相同定义的答案:

class Page1(Tkinter.Frame):
    def __init__(self, parent, controller):
        Tkinter.Frame.__init__(self, parent)


class Page2(Tkinter.Frame):
    def __init__(self, parent, controller):
        Tkinter.Frame.__init__(self, parent)

and so on...

等等……

You can see that all the page class have the same ascending order (self, parent, controller). More than that, (self,parent) in the 3rd line of every page are same in order for the program to run.

您可以看到,所有的页面类都具有相同的升序(self, parent, controller)。在每个页面的第三行中,(self,parent)都是相同的,以便程序运行。

Otherwise, it will not run or give an error about the arguments.

否则,它将不会运行或给出关于参数的错误。

#2


1  

Simply put, when you are calling the F(self, container) constructor, you are passing two arguments to the constructor, but Python also includes the newly created object as it's first parameter which is why it's telling you three arguments were given.

简单地说,当您调用F(self, container)构造函数时,您正在将两个参数传递给构造函数,但是Python还将新创建的对象包含为它的第一个参数,这就是为什么它告诉您三个参数的原因。

Look at this example:

看看这个例子:

class Foo(object):
    def __init__(self, bar):
        self.bar = bar

foobar = Foo('bar')
print(foobar.bar)

This creates a new object of type Foo, and prints the value of the bar on the new object. Here is the output:

这将创建一个Foo类型的新对象,并打印新对象上的bar的值。这是输出:

bar

Note how the __init__ method is declared with two arguments, but when creating a new object with Foo('bar'), we are only calling it with one argument.

注意__init__方法是如何用两个参数声明的,但是当用Foo('bar')创建一个新对象时,我们只使用一个参数调用它。

The constructor requires two arguments, but the first is going to be the instance of the object being created. The rest of the arguments passed will be whatever is passed when calling the constructor.

构造函数需要两个参数,但第一个参数将是创建对象的实例。传递的其余参数将是调用构造函数时传递的任何内容。

So in your case, the Page1 and Page2 classes have an __init__ method with two arguments, which means that you need to call it with one argument, because the first one is automatically the new instance of the respective class.

因此,在您的示例中,Page1和Page2类有一个带有两个参数的__init__方法,这意味着您需要用一个参数来调用它,因为第一个参数自动是相应类的新实例。

#1


1  

@fhdrsdg has pointed out the answer where all the class added should have same definition like this :

@fhdrsdg已经指出了所有添加的类都应该具有相同定义的答案:

class Page1(Tkinter.Frame):
    def __init__(self, parent, controller):
        Tkinter.Frame.__init__(self, parent)


class Page2(Tkinter.Frame):
    def __init__(self, parent, controller):
        Tkinter.Frame.__init__(self, parent)

and so on...

等等……

You can see that all the page class have the same ascending order (self, parent, controller). More than that, (self,parent) in the 3rd line of every page are same in order for the program to run.

您可以看到,所有的页面类都具有相同的升序(self, parent, controller)。在每个页面的第三行中,(self,parent)都是相同的,以便程序运行。

Otherwise, it will not run or give an error about the arguments.

否则,它将不会运行或给出关于参数的错误。

#2


1  

Simply put, when you are calling the F(self, container) constructor, you are passing two arguments to the constructor, but Python also includes the newly created object as it's first parameter which is why it's telling you three arguments were given.

简单地说,当您调用F(self, container)构造函数时,您正在将两个参数传递给构造函数,但是Python还将新创建的对象包含为它的第一个参数,这就是为什么它告诉您三个参数的原因。

Look at this example:

看看这个例子:

class Foo(object):
    def __init__(self, bar):
        self.bar = bar

foobar = Foo('bar')
print(foobar.bar)

This creates a new object of type Foo, and prints the value of the bar on the new object. Here is the output:

这将创建一个Foo类型的新对象,并打印新对象上的bar的值。这是输出:

bar

Note how the __init__ method is declared with two arguments, but when creating a new object with Foo('bar'), we are only calling it with one argument.

注意__init__方法是如何用两个参数声明的,但是当用Foo('bar')创建一个新对象时,我们只使用一个参数调用它。

The constructor requires two arguments, but the first is going to be the instance of the object being created. The rest of the arguments passed will be whatever is passed when calling the constructor.

构造函数需要两个参数,但第一个参数将是创建对象的实例。传递的其余参数将是调用构造函数时传递的任何内容。

So in your case, the Page1 and Page2 classes have an __init__ method with two arguments, which means that you need to call it with one argument, because the first one is automatically the new instance of the respective class.

因此,在您的示例中,Page1和Page2类有一个带有两个参数的__init__方法,这意味着您需要用一个参数来调用它,因为第一个参数自动是相应类的新实例。