Python Forum
Generating menus with classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating menus with classes
#1
Star 
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)# Smile". 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()
Reply
#2
I don't know why you can call a class without instantiating it, but you can.

class test :
	def __init__ (self) :
		print ('I am ALIVE!')

test ()
Reply
#3
In the code below, test() creates an instance of class test. It doesn't create a variable to reference the instance, but it still creates a test object.
class test :
    def __init__ (self) :
        print ('I am ALIVE!')
 
test()
The only difference between "test()" and "a = test()" is that "a = test()" creates a variable named "a" in the current scope and assigns it to reference the object return by test(). With or without a variable to reference it the object is created the same.

The same is true for HeyJoe's example. This code in Main.__init__ creates an instance of FirstMenu. Then the FirstMenu.__init__() create instances of SecondMenu which in turn creates an instance of ThirdMenu. There are no variables that reference the created objects, but the objects are still created. You should think about reworking the code. I don't think it is a good idea cascading code like that, and you obviously don't understand how it works. That is never a good thing.

I don't know where you got the idea that you need instances to use classes. A class can consist completely of class attributes and class variables and act just like a module.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Which is best GUI for drop down menus and calculations Chuck_Norwich 2 2,027 Sep-27-2019, 05:59 PM
Last Post: Chuck_Norwich
  Binding functions to Menus in tkinter?? Mocap 1 2,424 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  Menus and icons changed after upgrade blackclover 0 1,904 May-11-2018, 08:14 PM
Last Post: blackclover

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020