pythonpython-3.xtkintertkinter.optionmenu

How to know all the disabled options in an Options Menu Tkinter Closed


I have disabled an option in an OptionMenu to fix something in the code where if I click on it just duplicates it, so I thought by disabling the option is an easy work around, but when the user may want to change to another option both the options are now disabled making the first one not usable again. I now need to return the option to normal. I thought of getting all the options which are disabled but couldn't figure out on how to do that. Sorry for the long para.

Any suggestions are useful.


Solution

  • Assume optmenu is the instance of OptionMenu, the following code will return all the items that are disabled:

    # optmenu is the instance of OptionMenu
    menu = optmenu['menu']
    items = []  # list to hold all disabled items
    # go through all option items
    for i in range(menu.index('end')+1):
        if menu.entrycget(i, 'state') == 'disabled':
            items.append(menu.entrycget(i, 'label'))
    print(items)