I currently write an application and created three classes for it so far. The first class contains all the functions. The second class contains the 'window' widget and the third class contains the navigation bar.
When I inherit from the second class the first class, the button works and executes the functions. The moment I inherit from the navigation class the window class, through which I inherit also the function class, all of a sudden my widgets double. The buttons work (both buttons work) but obviously I do not want it doubled.
Can somebody explain that to me?
This is the output with inheritance from Start to FunctionClass
This is the output when TopMenu inherits from Start and Start from FunctionClass
My code is:
from tkinter import *
class FunctionClass:
def print_text(self):
print("This is an example!")
class Start(FunctionClass):
def __init__(self, master):
super().__init__()
frame = Frame(master)
frame.pack()
self.label = Label(frame,text="This is just a label.").pack()
self.label2 = Label(frame, text="This is second label.").pack()
self.button = Button(frame, text="Magic Button", command=self.print_text).pack()
class TopMenu(Start):
def __init__(self, master):
super().__init__(master)
# *******Top-Navigation Bar**********
tnb = Menu(master)
root.config(menu=tnb)
# *******tnb_file*******
tnb_file = Menu(tnb, tearoff=0)
tnb.add_cascade(label="File", menu=tnb_file)
tnb_file.add_command(label="Button", command=self.print_text)
tnb_file.add_separator()
tnb_file.add_command(label="Exit", command=root.destroy)
root = Tk()
g = Start(root)
d = TopMenu(root)
root.mainloop()
Inheritance means is a. if "Dog" inherits from "Animal", "Dog" is a "Animal".
When TopMenu
inherits from Start
, TopMenu
is a Start
. Anything Start
does or can do is done or can be done with TopMenu
. Thus, because Start
creates some widgets, anything that inherits from Start
will also create widgets.
When you do this:
g = Start(root)
d = TopMenu(root)
... you first create widgets with g = Start(root)
, and then you create the widgets again when you do d = TopMenu
. Again, this is because TopMenu
_is a Start
.