Hello, im currently working on a program and im trying to create a cascade, This cascade code works by itself but i want to implement def hello and def toggle into def __init__(self, master=None), at the moment im getting and error "UnboundLocalError: local variable 'submenu' referenced before assignment" does anyone know how i could fix this? I would like the code to stay inside the def __init__ because i will be adding other functions into this.

from Tkinter import *

class Application(Frame):
    def __init__(self, master=None):
        self.master = master=None
        self.menubar = Menu(self.master)
        Frame.__init__(self)
        self.grid()
        self.toggle()
        self.hello()
   
    def hello(self):
        print "hello !"

    def toggle(self):
        if submenu.entrycget(0,"state")=="normal":
            submenu.entryconfig(0,state=DISABLED)
            submenu.entryconfig(1,label="Speak please")
        else:
            submenu.entryconfig(0,state=NORMAL)
            submenu.entryconfig(1,label="Quiet please")

        menubar = Menu(self)

        submenu=Menu(menubar,tearoff=0)

        submenu2=Menu(submenu,tearoff=0)
        submenu2.add_command(label="hello", command=meat)

        # this cascade will have index 0 in submenu
        submenu.add_cascade(label="Say",menu=submenu2,state=DISABLED)
        # these commands will have index 1 and 2
        submenu.add_command(label="Speak please",command=toggle)
        submenu.add_command(label="Exit", command=self.quit)

        menubar.add_cascade(label="Test",menu=submenu)

        ## display the menu
        self.config(menu=menubar)
        self.mainloop()

app = Application()
app.master.title("Major Project Sample Screen")
app.mainloop()

I was thinking that return might work??
how would i go about doing this if i am right?

5th line.
It should be : self.master = master not self.master = master=None You can't assign something and assign it again in 1 statemnet.

5th line.
It should be : self.master = master not self.master = master=None You can't assign something and assign it again in 1 statemnet.

Yes, you can do
self.master = master = None
It simply means that self.master and master are both None. May not be what you want!

To wit ...

x = y = z = 0

print x, y, z  # 0 0 0

Edited 6 Years Ago by vegaseat: n/a

oh wow i didn;t know you can't do that in python. I was taught saying that's bad practise..

It's not anti-idiomatic, I think.

I would call assigning None over given parameter to function very bad style, sorry to say so. I also do not like local variables never used after assignment like this parameter master.

I fixed my other error but now i get the following error, TclError: unknown option "-menu", i have no idea what this error means could someone please help me out here??

from Tkinter import *

class Application(Frame):
    def __init__(self, master=None):
        self.master = master
        self.menubar = Menu(self.master)
        Frame.__init__(self)
        self.grid()
        self.toggle()
        self.hello()

        menubar = Menu(self)

        submenu=Menu(menubar,tearoff=0)

        def hello(self):
            print "hello !"

        def toggle(self):
            if submenu.entrycget(0,"state")=="normal":
                submenu.entryconfig(0,state=DISABLED)
                submenu.entryconfig(1,label="Speak please")
            else:
                submenu.entryconfig(0,state=NORMAL)
                submenu.entryconfig(1,label="Quiet please")

        submenu2=Menu(submenu,tearoff=0)
        submenu2.add_command(label="hello", command=hello)

        # this cascade will have index 0 in submenu
        submenu.add_cascade(label="Say",menu=submenu2,state=DISABLED)
        # these commands will have index 1 and 2
        submenu.add_command(label="Speak please",command=toggle)
        submenu.add_command(label="Exit", command=self.quit)

        menubar.add_cascade(label="Test",menu=submenu)
        

        ## display the menu
        self.config(menu=menubar)
        self.mainloop()
     

app = Application()
app.master.title("Sample Screen")
app.mainloop()

sorry wrong error this is the error
AttributeError: Application instance has no attribute 'toggle'

sorry wrong error this is the error
AttributeError: Application instance has no attribute 'toggle'

"toggle" is no longer a member of Tkinter. You re-defined it as a member of your Application class.

Edited 6 Years Ago by woooee: n/a

hey, i think you need to put in self...

submenu.add_command(label="Speak please",command=self.toggle)

it should work after that
:)

It tells that self has not definition for attribute called toggle, as __init__ is only attribute in the object. And next error will be missing self. part of submenu assignment :(

Edited 6 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.