TransWikia.com

TypeError: __init__() takes 2 positional arguments but 3 were given - History and Settings classes should be well implemented?

Stack Overflow Asked by chessguy on December 3, 2021

I have one class I need two separate into two classes.

WIDTH=50
HEIGHT=10

class History(Frame):
    def __init__(self, parent):
        super().__init__(parent, borderwidth=1)

        self.container = Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.container.grid_rowconfigure(0, weight=0)
        self.container.grid_columnconfigure(0, weight=0)

        Label(self.container, fg="white",text="History", bg='#3780ba', anchor='w', font=("Calibri", 12), width=WIDTH).grid(row=0, column=0, sticky=NSEW)
        Label(self.container, fg="white",text="Settings", bg='#3780ba', anchor='w', font= ("Calibri", 12) , width=WIDTH ).grid(row=2, column=0, sticky=NSEW)

        text_widget = Text(self.container, height=HEIGHT, width=WIDTH).grid(row=1, column=0)
        text_widget = Text(self.container, height=HEIGHT, width=WIDTH).grid(row=3, column=0)

This class is a bit clear. I prefer to separate into two classes. Here is my attempt :

class History(Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.container = Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.container.grid_rowconfigure(0, weight=0)
        self.container.grid_columnconfigure(0, weight=0)

        Label(self.container, fg="white",text="History", bg='#3780ba', anchor='w', font=("Calibri", 12), width=WIDTH).grid(row=0, column=0, sticky=NSEW)
        Text(self.container, height=HEIGHT, width=WIDTH).grid(row=1, column=0)


class Settings(History):
    def __init__(self, parent):
        super().__init__(self, parent)

        Label(self.container, fg="white", text="Settings", bg='#3780ba', anchor='w', font= ("Calibri", 12) , width=WIDTH ).grid(row=2, column=0, sticky=NSEW)
        Text(self.container, height=HEIGHT, width=WIDTH).grid(row=3, column=0)

With

    self.history = History(self)
    self.history.grid(row=0, column=1)

    self.settings = Settings(self)
    self.parametres.grid(row=1, column=1)

I got the following error :

Traceback (most recent call last):
  File "test.py", line 161, in <module>
    root = Game()
  File "test.py", line 66, in __init__
    self.settings = Settings(self)
  File "tests.py", line 26, in __init__
    super().__init__(self, parent)
TypeError: __init__() takes 2 positional arguments but 3 were given

I am confused … Why my classes History and Settings are not well implemented?

One Answer

The problem lies in the constructor for your Settings class, specifically:

super().__init__(self, parent)

When you call a method of an object, the object is implicitly added as the first parameter to the call. That means you are effectively passing two copies of self and one of parent, resulting in the error "I wanted two but you gave me three, so go away" (paraphrased).

In other words, do not explicitly pass self (match what you do in your History class):

super().__init__(parent)

Answered by paxdiablo on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP