Tkinter: disable menu items while running

Pierre Quentel quentel.pierre at wanadoo.fr
Fri Jan 16 06:40:08 EST 2004


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





More information about the Python-list mailing list