Beginner's help on Super within Tkinter.
I am trying to understand how to utilize super() within a tkinter app. I need to reference attributes from parent class. From code, a button press with super().init called, calls another parent and does not reference the info as needed. with super().init not called, i.e. commented out, it throws an attribute error. Can someone tell me how to call super attributes without rewriting (and overwriting) the original information.
import tkinter as tk
class Parent():
def __init__(self,master):
self.sym = 'tree'
self.cl = 3
self.enter = tk.Entry(master)
self.enter.pack()
self.button = tk.Button(master,text='text', command= lambda: Child(master))
self.button.pack()
class Child(Parent):
def __init__(self,master):
# super().__init__(master)
print(self.enter.get(), self.cl)
root = tk.Tk()
a = Parent(root)
root.mainloop()
What you seem to be doing isn't what subclassing is for. You're trying to use a new instance of a class to get the attributes of a different instance of a parent class.
The proper way to do subclassing is to not create an instance of Parent
. Instead, create an instance of Child
. With that, the instance of Child
will have access to everything in the parent class definition.
Example:
import tkinter as tk
class Parent():
def __init__(self,master):
self.sym = 'tree'
self.cl = 3
self.enter = tk.Entry(master)
self.enter.pack()
self.button = tk.Button(master,text='text', command= self.print_value)
self.button.pack()
class Child(Parent):
def __init__(self,master):
super().__init__(master)
print(self.enter.get(), self.cl)
def print_value(self):
print(self.enter.get())
root = tk.Tk()
a = Child(root)
root.mainloop()