Tkinter - Disable Menu Item
Disable Menu Item in Tkinter
To disable a menu item in Tkinter, set the state of the menu item to disabled using Menu.entryconfig() method.
For example, if there is a menu item with the label "Item3" in the menu_1 Menu object, then the syntax to disable the menu item is
menu_1.entryconfig("Item3", state="disabled")
For the first argument, instead of the label string, you can also pass the index of the item in the menu to disable the menu item.
In this tutorial, you will learn how to disable a menu item in Tkinter, as shown in the following example menu.
Menu1 # Menu1 is a menu in menu bar
Item1 # item under Menu1
Item2 # item under Menu1
"Item3" # disabled item under Menu1
Item4 # item under Menu1
Example
In this example, we shall create a window, create a menu bar, create a menu, say Menu1, add few items to this Menu1, and then disable a menu item "Item3".
Python Program
import tkinter as tk
# Create the main window
window = tk.Tk()
# Create the menu bar
menu_bar = tk.Menu(window)
# Create the menu Menu1
menu_1 = tk.Menu(menu_bar, tearoff=0)
# Add items for Menu1
menu_1.add_command(label="Item1")
menu_1.add_command(label="Item2")
menu_1.add_cascade(label="Item3")
menu_1.add_cascade(label="Item4")
# Disable the "Item3" menu item
menu_1.entryconfig("Item3", state="disabled")
# Add the menu to the menu bar
menu_bar.add_cascade(label="Menu1", menu=menu_1)
# Attach the menu bar to the main window
window.config(menu=menu_bar)
# Start the Tkinter event loop
window.mainloop()
Output
Summary
In this Python Tkinter tutorial, we learned how to disable a menu item in Tkinter, with the help of examples.