Tkinter: disable menu items while running

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick L. Nolan

    #1

    Tkinter: disable menu items while running

    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.entryconfig ure().
    However, I don't know how to get ahold of the objects
    created by add_cascade() or add_command(). Do they live
    somewhere accessible?

    --
    * Patrick L. Nolan *
    * W. W. Hansen Experimental Physics Laboratory (HEPL) *
    * Stanford University *
  • Pierre Quentel

    #2
    Re: Tkinter: disable menu items while running

    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.entrycg et(0,"state")== "normal":
    submenu.entryco nfig(0,state=DI SABLED)
    submenu.entryco nfig(1,label="S peak please")
    else:
    submenu.entryco nfig(0,state=NO RMAL)
    submenu.entryco nfig(1,label="Q uiet please")

    menubar = Menu(root)

    submenu=Menu(me nubar,tearoff=0 )

    submenu2=Menu(s ubmenu,tearoff= 0)
    submenu2.add_co mmand(label="He llo", command=hello)

    # this cascade will have index 0 in submenu
    submenu.add_cas cade(label="Say ",menu=submenu2 ,state=DISABLED )
    # these commands will have index 1 and 2
    submenu.add_com mand(label="Spe ak please",command =toggle)
    submenu.add_com mand(label="Exi t", command=root.qu it)

    menubar.add_cas cade(label="Tes t",menu=submenu )

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


    Comment

    Working...