Python tkinter disabling drop down options across all options, even after other options are chosen. But enabled again, when option is repeated

Question:

This is the code I have so far:

from tkinter import *
from tkinter import ttk, messagebox


# Create the Frame and menubar
root = Tk()
root.title('Example Run')
root.iconbitmap('') #None for now, specifying directory on computer
root.geometry("1000x500")

#Create Menu
menubar = Menu(root)
root.config(menu=menubar)

# Create the menu widgets
First = Menu(menubar, tearoff = False)
Second = Menu(menubar, tearoff = False)
Third = Menu(menubar, tearoff = False)
Fourth = Menu(menubar, tearoff = False)
Fifth = Menu(menubar, tearoff = False)
Sixth = Menu(menubar, tearoff = False)


Menu_Choices = [First, Second, Third, Fourth, Fifth, Sixth]
    
# Add menu widgets to the menubar
menubar.add_cascade(menu=First, label="First")
menubar.add_cascade(menu=Second, label="Second")
menubar.add_cascade(menu=Third, label="Third")
menubar.add_cascade(menu=Fourth, label="Fourth")
menubar.add_cascade(menu=Fifth, label="Fifth")
menubar.add_cascade(menu=Sixth, label="Sixth")

# Define the names of each menu
First_list = ['1','2','3','4','5']
Second_list = ['2','4','6','8','10']
Third_list = ['3','6','9','12','18']
Fourth_list = ['1','3','5','7','9']
Fifth_list = ['2','3','5','7','8']
Sixth_list = ['END']
menu_list_items = [First_list, Second_list, Third_list, Fourth_list, Fifth_list, Sixth_list]

# Add menu items to the menu widgets
for i, l in enumerate(menu_list_items):
    for item in l:
        Menu_Choices[i].add_command(label=item)
        
root.mainloop()

I am trying to make a menu bar interface that can take into account what the end user is pressing on, and disable that option from being pressed in other menu bars.

Example Scenario:
If I press ‘2’ in the "First" drop down menu, then the ‘2’ option is no longer available (disabled) for the ‘Second’, and ‘Fifth’ drop down menu. However, ‘2’ is still present in the "First" drop down menu, the menu I had clicked on.

Moving on, I decide to press ‘5’ in the ‘Fourth’ drop down menu…Now ‘5’ option is no longer available (disabled) for ‘First’ and ‘Fifth’ drop down menu. Along with, the ‘2’ option is no longer available (disabled) for the ‘Second’, and ‘Fifth’ drop down menu, as we pressed that before. However, ‘2’ is still present in the "First" drop down menu, the menu I had clicked on early on. Along with ‘5’ in the ‘Fourth’ drop down menu, which I clicked on early on.

Lastly, I go back to the ‘First’ drop down menu…I decided to click on ‘3’. Now, ‘2’ is enabled in all drop menus since I changed the option in the first drop down menu. But, ‘3’ is disabled in the ‘Third’, ‘Fourth’, and ‘Fifth’ drop down menus. ‘3’ is still an option for the ‘First’ drop down menu since I clicked on it there. But again, ‘5’ option is still no longer available (disabled) for ‘First’ and ‘Fifth’ drop down menu, having clicked on it in the ‘Fourth’ drop down menu in my second click.

Does that make sense?
I know I’m going to have to make functions for this. But this is my first time using tkinter library, and it’s becoming overwhelmingly difficult to understand the syntax and logic behind it. I know the steps I need to take, but transferring that into logical syntax is what is killing me. I’m using the following documentation website:
https://docs.python.org/3/library/tk.html

Asked By: PKrange

||

Answers:

This does what you ask. The principle here is that we add a callback to each menu item, and pass the menu number and the menu item. Then, we iterate through the menus and disable that item.

This was made more tricky because your sample menu items are all integer (as strings). The primitive Tk APIs will interpret those integers as position numbers, not as menu labels. If you had used letters, we wouldn’t need the index calls – we could just do menu.entryconfig(item, state=DISABLED).

...
selected = []

def select(i, item):
    n = menu_list_items[i].index(item)
    selected.append(item)
    for items,menu in zip(menu_list_items,Menu_Choices):
        if item in items and menu != Menu_Choices[i]:
            menu.entryconfig(items.index(item), state=DISABLED)

# Add menu items to the menu widgets
for i,l in enumerate(menu_list_items):
    for item in l:
        Menu_Choices[i].add_command(label=item, command=lambda i=i, item=item: select(i,item))
Answered By: Tim Roberts
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.