Check whether a Menu item is enabled or disabled in tkinter

Viewed 378

Is there any way to check whether a menu item is enabled or not in tkinter?

Here's the code:

from tkinter import *

root = Tk()
root.geometry("500x500")

def disable_menus():
    test_menu.entryconfig("Sub-Menu 1" , state=DISABLED)
    test_menu.entryconfig("Sub-Menu 2" , state=DISABLED)

def enable_menus():
    test_menu.entryconfig("Sub-Menu 1", state="normal")
    test_menu.entryconfig("Sub-Menu 2", state="normal")

def check_state():
    # Code to check if the sub-menus are enabled or not
    pass

disable_button = Button(root , text = "Disable Menus" , command = disable_menus)
disable_button.grid(row = 0 , column = 0 , padx = 20 , pady = 20)

enable_button = Button(root , text = "Enable Menus" , command = enable_menus)
enable_button.grid(row = 1 , column = 0)

check_button = Button(root , text = "Check State" , command = check_state)
check_button.grid(row = 0 , column = 1)

main_menu = Menu(root)
root.config(menu=main_menu)

test_menu = Menu(main_menu)
main_menu.add_cascade(label = "Test Menu 1" , menu = test_menu)

test_menu.add_command(label = "Sub-Menu 1")
test_menu.add_command(label = "Sub-Menu 2")

mainloop()

Here when I click on the check_button, I want to check whether the menu items are enabled or not.

Is there any way to achieve this in tkinter?

It would be great if anyone could help me out?

1 Answers

Use menu.entrycget(index, option) this will return the value of the option. In your case:

test_menu.entrycget("Sub-Menu 1", 'state')

This will return the state of the "Sub-Menu 1".

Related