Search code examples
pythonunixtkinteroptionmenu

How to disable a tkinter OptionMenu


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)

Solution

  • 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.