1

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

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)
mgilson
  • 300,191
  • 65
  • 633
  • 696
Nicholas Tan
  • 13
  • 1
  • 4
  • 1
    `F(container)` ... you should really go read some tutorials about classes in python... self is always implicitly passed to instanciated class methods – Joran Beasley May 04 '15 at 16:38
  • But after i remove the "self", the GUI didn't come out. – Nicholas Tan May 04 '15 at 16:43
  • 1
    @JoranBeasley: or `F(self)` if `self` is to be the parent. – Martijn Pieters May 04 '15 at 16:43
  • @NicholasTan since i cant copy and paste your code to get the same error I cant help you any more than my previous advice to read some tutorials on the subject :) – Joran Beasley May 04 '15 at 16:46
  • I remove either `self` or `container` also can't – Nicholas Tan May 04 '15 at 16:46
  • @Joran Beasley Ok i will – Nicholas Tan May 04 '15 at 16:46
  • It looks like your definition of Page1 or Page2 is wrong and accepts only `self` and one other argument. Without those definitions it's hard to help. Also, I'm guessing you took [this code](http://stackoverflow.com/a/7557028/3714930) as starting point? It would have been nice to point that out. Just look at the differences between what you have and that code that works perfectly. – fhdrsdg May 04 '15 at 16:52
  • @fhdrsdg i didnt get that code as starting point but i will refer to that code – Nicholas Tan May 04 '15 at 16:55
  • Like I said, look at the differences between that and the original code. You have `def __init__(self, controller): ` where it should be `def __init__(self, parent, controller): `. Or, because you use `frame = F(self, container)` you could make it `def __init__(self, controller, parent): `. Also, adding code in comments is not very readable, you should edit your question to include new information. – fhdrsdg May 04 '15 at 16:59

2 Answers2

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.

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:

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.

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.

Jazzer
  • 2,993
  • 1
  • 19
  • 24
1

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

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.

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

CodingBeginner
  • 768
  • 1
  • 5
  • 6