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


Messages In This Thread
Generating menus with classes - by Heyjoe - Feb-11-2021, 01:48 AM
RE: Generating menus with classes - by BashBedlam - Feb-11-2021, 02:41 AM
RE: Generating menus with classes - by deanhystad - Feb-11-2021, 05:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Which is best GUI for drop down menus and calculations Chuck_Norwich 2 2,087 Sep-27-2019, 05:59 PM
Last Post: Chuck_Norwich
  Binding functions to Menus in tkinter?? Mocap 1 2,476 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  Menus and icons changed after upgrade blackclover 0 1,940 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