如何在OptionMenu中禁用单个选项?

时间:2016-04-04 23:43:47

标签: python python-3.x tkinter

如果用户从OptionMenu列表中选择一个选项,则该选项将被禁用。

示例:如果用户选择" a"选项" a"应该被禁用,然后用户只能选择b或c。我尝试了下面的代码,但它返回错误,如图所示。我不想要menubuttonOptionMenu符合我的目的,但不是menubutton

import tkinter as tk
from tkinter import *

class Disable_state(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        va = StringVar()
        NAMES = { 'a' : 0, 'b' : 1, 'c' : 2 }
        op = list(NAMES.keys())
        self.menu = OptionMenu(self.parent, va, *op,command = lambda op: self.disable_option(op))
        self.menu.grid(column=0, row=0)
    def disable_option(self,op):
        self.menu.config(op, state = "disabled")


if __name__ =="__main__":
    root= Tk()
    o = Disable_state(root)
    root.mainloop()

错误是

self.menu.config(op, state = "disabled")
  File "C:\Python34\lib\tkinter\__init__.py", line 1319, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python34\lib\tkinter\__init__.py", line 1303, in _configure
    cnf = _cnfmerge((cnf, kw))
  File "C:\Python34\lib\tkinter\__init__.py", line 105, in _cnfmerge
    cnf.update(c)
ValueError: dictionary update sequence element #0 has length 1; 2 is  required    

1 个答案:

答案 0 :(得分:6)

似乎可以访问子窗口小部件menu,并使用entryconfigure方法可以禁用某个条目

self.menu['menu'].entryconfigure(op, state = "disabled")