How to disable a tkinter OptionMenu
Asked Answered
C

1

5

I can't figure out or find how to disable a tkinter OptionsMenu. I have 3 optionsmenu's in my GUI and want to disable them when a button is clicked

self.menu = OptionMenu(self, var, *items)
btn = Button(self, text="disable", command = self.disable)
btn,pack()

self.disable(self):
    //Disable menu here...

Is there a way to just call a built in function for OptionMenu and disable it? Or do I have to disable every option in the menu? (Which i also can't figure out)

BTW: I used the menu.pack() for a separate Topleve() window that pops up, but I started off with the grid() system in my main Tk window, used by menu.grid(row=0,column=0)

EDIT: So I forgot to mention that I have multiple OptionMenus being generated by a constructor method. This is what I tried doing and didn't work:

makeMenu():
    menu = OptionMenu(self, var, *items)
    ....//whole bunch of menu settings
    return menu

menu1 = makeMenu()
all_menus.append(menu)

Now the reason this didn't work is because I had to append it after creation. I don't know why the settings don't carry over, but what I had to do is this: makeMenu(): menu = OptionMenu(self, var, *items) ....//whole bunch of menu settings return menu

makeMenu():
    menu = OptionMenu(self, var, *items)
    ....//whole bunch of menu settings
    all_menus.append(menu)

makeMenu()

And with this change, I can use this to disable menus later on:

for menu in all_menus:
   menu.config(state=DISABLED)
Comehither answered 22/8, 2017 at 21:42 Comment(6)
Possible duplicate of How do I disable all the user input widgets (buttons,entries..) from a parent widget?Villainous
Close. But this has to do with OptionMenu's which work a little differently. I used that question for my input entries. It works! But unfortunately it doesn't with OptionMenu's. Thanks for the heads up though!Comehither
I tested it and it does work on OptionMenus. Admittedly there's no optical clue, but if you click a disabled OptionMenu nothing happens.Villainous
Ok. I'll give it another shot. If it still doesn't work, i'll post my attempt as an edit above.Comehither
If that wasn't clear, the "parent widget" isn't the OptionMenu. An OptionMenu has no child widgets. I was thinking of your 3 OptionMenus as the children of some container.Villainous
It worked @Rawing!!!!!! Thank you!Comehither
M
14

Like with any other widget, you use the configure method to set the state to "disabled":

self.menu.configure(state="disabled")

The above will work for both the tkinter and ttk OptionMenu widgets.

Marilyn answered 22/8, 2017 at 22:4 Comment(2)
On a side note, for some reason, the other state is called 'active' and not 'enabled' 🤷‍♂️Candice
@TheTomer: the valid states are active, normal, and disabled.Marilyn

© 2022 - 2024 — McMap. All rights reserved.