Hello fellow Python Users,
You need to make an object to use a class. I did make an object in the third line from the last "m=Main(root)#
". However, I don't understand why I was able to generate menus from the following classes, since I did not make an object for them. The classes are FirstMenu, SecondMenu, ThirdMenu.
Wouldn't I need something like:
f = FirstMenu(root)
s = SecondMenu(root)
t = ThirdMenu(root)
My code works but I would like to understand it better. Why was I able to get the menus to work without making an object for each class?
You need to make an object to use a class. I did make an object in the third line from the last "m=Main(root)#
". However, I don't understand why I was able to generate menus from the following classes, since I did not make an object for them. The classes are FirstMenu, SecondMenu, ThirdMenu.Wouldn't I need something like:
f = FirstMenu(root)
s = SecondMenu(root)
t = ThirdMenu(root)
My code works but I would like to understand it better. Why was I able to get the menus to work without making an object for each class?
import tkinter as tk
root = tk.Tk()
myfont = "helvitica 25"
mybg = "light sky blue"
myheight = 4
mywidth = 60
myheight2 = 2
mywidth2 = 20
myheight3 = 2
mywidth3 = 20
labcol = "spring green"
anscol = "yellow"
otherbutcol = "skyblue"
commandbutcol = "light sky blue"
mypadx = 0
mypady = 0
class Main:
def __init__(self, root):
self.root = root
root.geometry('1200x1200')
self.theframe = tk.Frame(root)
self.theframe.pack()
FirstMenu(root)
def desframe(self):
self.theframe.destroy()
def iw(self):
pass
class FirstMenu:
def __init__(self, root):
self.root = root
root.geometry('1200x1200')
self.theframe = tk.Frame(root)
self.theframe.pack()
self.fstmenu()
def fstmenu(self):
self.fstlabel = tk.Label(self.theframe, text="First Label", bg=["springgreen"], font=myfont,
relief="groove")
self.fstlabel.config(height=myheight, width=mywidth)
self.fstlabel.pack()
self.fstbutton = tk.Button(self.theframe, text="Click here to start.", bg=["springgreen"], font=myfont,
relief="groove", command=lambda: [self.desframe(),SecondMenu(root)])
self.fstbutton.config(height=myheight, width=mywidth)
self.fstbutton.pack()
def desframe(self):
self.theframe.destroy()
class SecondMenu:
def __init__(self, root):
self.root = root
root.geometry('1200x1200')
self.theframe = tk.Frame(root)
self.theframe.pack()
self.secmenu()
def secmenu(self):
self.seclabel = tk.Label(self.theframe, text="Second Label", bg="red", font=myfont,
relief="groove")
self.seclabel.config(height=myheight, width=mywidth)
self.seclabel.pack()
self.secbutton = tk.Button(self.theframe, text = "This is your second click.", bg="red", font=myfont,
relief="groove", command=lambda: [self.desframe(),ThirdMenu(root)])
self.secbutton.config(height=myheight, width=mywidth)
self.secbutton.pack()
def desframe(self):
self.theframe.destroy()
class ThirdMenu:
def __init__(self, root):
self.root = root
root.geometry('1200x1200')
self.theframe = tk.Frame(root)
self.theframe.pack()
self.thirdmenu()
def thirdmenu(self):
self.thirdlabel = tk.Label(self.theframe, text="Third Label", bg="purple", font=myfont,
relief="groove")
self.thirdlabel.config(height=myheight, width=mywidth)
self.thirdlabel.pack()
self.thirdbutton = tk.Button(self.theframe, text = "This is your third click.", bg="purple",font = myfont, command=lambda: [self.desframe(),ThirdMenu(root)])
self.thirdbutton.config(height=myheight, width=mywidth)
self.thirdbutton.pack()
def desframe(self):
self.theframe.destroy()
m=Main(root)# **smile**
root.mainloop()
