简体   繁体   English

如何禁用tkinter OptionMenu

[英]How to disable a tkinter OptionMenu

I can't figure out or find how to disable a tkinter OptionsMenu. 我不知道或找不到如何禁用tkinter OptionsMenu。 I have 3 optionsmenu's in my GUI and want to disable them when a button is clicked 我的GUI中有3个选项菜单,想在单击按钮时禁用它们

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? 有没有办法只为OptionMenu调用内置函数并将其禁用? 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) 顺便说一句:我使用menu.pack()弹出一个单独的Topleve()窗口,但是我从主Tk窗口中的grid()系统开始,由menu.grid(row=0,column=0)

EDIT: So I forgot to mention that I have multiple OptionMenus being generated by a constructor method. 编辑:所以我忘了提到我有一个构造函数方法生成的多个OptionMenus。 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).... //整堆菜单设置都返回菜单

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)

Like with any other widget, you use the configure method to set the state to "disabled": 与任何其他小部件一样,您可以使用configure方法将状态设置为“禁用”:

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

The above will work for both the tkinter and ttk OptionMenu widgets. 上面的方法适用于OptionMenu和ttk OptionMenu小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM