1

When i try to start my script, i get a error. I have main class Page1 and inside is My_Data class and function1 function. The error is:

TypeError: Page1.__init__.<locals>.My_Data.__init__() got an unexpected keyword argument 'Name'

MyCode.py:

class Page1(tk.Frame):
    def __init__(self, master, other, **kw):
        super().__init__(master, **kw)

        self.configure(bg='white')

        class My_Data():
            def __init__(self):
                self.Name: str
                self.Year: float
                

        def function1(self):

            My_Dictionary = {}
            x = cursor.execute("sql")


  • 1
    The `def __init__(self):` in the `class My_Data():` block is missing the `Name` and `Year` argument definition (only `self` is required). Please [reference this answer](https://stackoverflow.com/a/625097/) for what you might be trying to do. – metatoaster Jan 25 '23 at 03:14
  • @metatoaster I'm sorry I did not understand. How can I fix? –  Jan 25 '23 at 03:22
  • 1
    You should start with a [basic tutorial](https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables) on how to create classes, with `__init__` that accept some arguments, and get back the attribute from it. Also you need to assign the arguments to `self.Name` and `self.Year`, the type annotation doesn't do any assignments. – metatoaster Jan 25 '23 at 03:31
  • There's rarely a good reason to nest classes in Python. A few frameworks use nested classes for special purposes, but in most code it just is more confusing than writing your classes at the top level of the module. The Python language itself doesn't consider a nested class to have any special meaning (it doesn't get special access to the surrounding class). – Blckknght Jan 25 '23 at 04:21

1 Answers1

1

It's because My_Data class doesn't have parameters Name and Year in its __init__ function. Just add the two params.

class My_Data():
    def __init__(self, Name, Year):
        self.Name = Name
        self.Year = Year
Lahcen YAMOUN
  • 657
  • 3
  • 15
  • Instead here I have to use ".self" for Name and Year? Data = My_data (Name = row [1], Year = Row [2]) –  Jan 25 '23 at 03:34
  • The `__init__` function won't recognize `Name` and `Year` keywords since you didn't provide the arguments in the definition of the function. Having `self.Name` doesn't assume the value will be passed via argument in the initiator. – Lahcen YAMOUN Jan 25 '23 at 03:39
  • So I have to use Data = My_data (self.Name = row[1], self.Year = Row[2]) ? Or do I write Data = My_data (Name = row [1], Year = Row [2])? Thank you –  Jan 25 '23 at 03:42
  • Keep `Data=My_Data(Name=row[1], Year=row[2])`, and change the `__init__` function to what I defined in my comment. – Lahcen YAMOUN Jan 25 '23 at 03:47