Tkinter: disable menu items while running

P

Patrick L. Nolan

We have a Tkinter application which has a menubar with cascade
submenus. I would like to start the program with one of the
submenu items state=DISABLED, then change it to state=NORMAL
at a later time. I hope I just missed something obvious,
because I can't figure out how to change its state.

The problem comes about because the menu bar is built by several
add_cascade() calls. Each of the cascades, in turn, has
several add_command() calls. If the cascade in question was,
say, an object named Cas, then I could call Cas.entryconfigure().
However, I don't know how to get ahold of the objects
created by add_cascade() or add_command(). Do they live
somewhere accessible?
 
P

Pierre Quentel

Menu widgets have an index, used to get/set their properties
There must be more elegant solutions, but this should work :

----------------------
from Tkinter import *

root=Tk()

def hello():
print "hello !"

def toggle():
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(root)

submenu=Menu(menubar,tearoff=0)

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=root.quit)

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

# display the menu
root.config(menu=menubar)
root.mainloop()
----------------------Hope this helps,
Pierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top