0

I am trying to inherit some values from one class to a another one. I am using the function super to inherit. Below is a simplfied version of my problem. Thank you for help.

from tkinter import *
import random
class First(object):
    def __init__(self,master):
        super(First, self).__init__(master)
    def random(self):
        self._y = random.randint(11,20)
        self._x = random.randint(1,10)
    def random2(self):
        s = First(root)
        s.random()


class Second(Frame,First):
    def __init__(self,master):
        super(Second, self).__init__(master)
        self.grid()
        self.menuFrame = Frame(self)
        self.create_menu_widgets()
        self.menuFrame.grid()
    def create_menu_widgets(self):
          btnMainMenu = Button(self.menuFrame,font=("consolas",18,"bold"),text="Main Menu")
          btnMainMenu.pack()
    def print(self):
        print(self._y,self._x)



root = Tk()
x = Second(root)
x.random()
x.random2()
x.print()
root.configure(background   = 'green')
root.mainloop()

I keep on getting the error:

super(First, self).__init__(master)
TypeError: object.__init__() takes no parameters

Please help me, I think the problem is where I have s=First(root). Thanks for help.

9
  • 1
    You don't need to call super if it is object. Commented Oct 25, 2018 at 13:06
  • 1
    Why are you trying to create a new instance of the class from inside the class? s = First(root)? Commented Oct 25, 2018 at 13:10
  • 2
    I think I get what you are trying to do in random2 but what you should be doing is calling self instead of creating a new instance. so delete this line: s = First(root) and change this line: s.random() to this: self.random() and you will get the same results without having to build a new instance of the class. Commented Oct 25, 2018 at 13:29
  • 1
    @AR_ we all have to start somewhere. I was not good at coding when I first started as well. But here I am 2 years later still learning and helping where I can :D. Just keep at it. You will get better. I added my answer as well to clear up some mistakes I noticed in your example. Let me know if you have any questions. Commented Oct 25, 2018 at 13:56
  • 1
    Inheritance isn't for sharing data between objects. Each instance is distinct and doesn't share information with the others. Commented Oct 25, 2018 at 14:04

2 Answers 2

2

When you call super on a class that is the highest in your hierarchy it will go object. object is the super class of all objects in Python. So super(First, self).__init__(master) will try to initialize the object not any of your classes. You can see this inheritance using the Class.__mro__. To figure out what I'm talking about.

And inheriting from object? That happens by default even if you don't specify anything. So I guess you wanted to inherit from Frame as object doesn't make any sense.

So change your code to this and it should be fixed.

from tkinter import *
import random
class First(Frame): # changed here
    def random(self):
        self._y = random.randint(11,20)
        self._x = random.randint(1,10)
    def random2(self):
        s = First(root)
        s.random()


class Second(First): # changed here 
    def __init__(self,master):
        super(Second, self).__init__(master)
        self.grid()
        self.menuFrame = Frame(self)
        self.create_menu_widgets()
        self.menuFrame.grid()
    def create_menu_widgets(self):
          btnMainMenu = Button(self.menuFrame,font=("consolas",18,"bold"),text="Main Menu")
          btnMainMenu.pack()
    def print(self):
        print(self._y,self._x)

root = Tk()
x = Second(root)
x.random()
x.random2()
x.print()
root.configure(background   = 'green') # you cannot see this as your button fills everything
root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

There are still a few minor problems Mike - SMT is addressing in comments above.
Thank you Sir, this helped me alot.
2

I see several issues in your example.

1:

you are assigning Second() to x but then calling x.random() and x.random2(). This will not work as your random methods only exist in the First() class.

2:

Don't name a function, method, variable or attribute the same thing as a built in method. This will cause problems.

Change your def print(self) to something like def my_print(self) or anything that is not exactly print. While we are talking about this print statement you only define self._x and self._y in your First() class but try to print them in your Second() class. This will never work. self is always a reference to the class object and never a reference to a class controller that was passed to the class.

Now I get what you are trying to do here and I will rebuild your code to show how to share information between classes.

You should not use a geometry manager fro inside the Frame class. Instead use it on the class variable name. This will allow you chose between any geometry manager for the class instead of sticking to just one kind.

As Vineeth pointed out you do not use supper for an object class.

The below code will run the Second() class and then when you want to reference the random methods on the First() class you can do so with the new methods I added to Second(). Let me know if you have any questions.

One last change is to import tkinter as tk this will help prevent accidentally overwriting an imported method from tkinter.

Here is a working example of your code:

import tkinter as tk
import random


class First(object):
    def random(self):
        return "From First.Random!", random.randint(11,20), random.randint(1,10)

    def random2(self):
        return "From First.Random2!", self.random()


class Second(tk.Frame):
    def __init__(self, master):
        super(Second, self).__init__(master)
        self.menuFrame = tk.Frame(self)
        self.menuFrame.grid()
        tk.Button(self.menuFrame, font=("consolas", 18, "bold"), text="Main Menu").pack()

    def random(self):
        print(First().random())
    def random2(self):
        print(First().random2())


root = tk.Tk()
root.configure(background='green')

x = Second(root)
x.pack()
x.random()
x.random2()

root.mainloop()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.