如何在Tkinter中禁用Combobox?

13
基本上,我希望根据另一个组合框的值禁用某个组合框。 我找不到这个问题的答案,可能是因为对组合框进行此操作非常罕见。
我有一段代码如下...
    self.cBox1Var=tki.StringVar()
    self.cBox1=ttk.Combobox(self.mframe, width=16, textvariable=self.cBox1Var, state='readonly',values=['Text entry','Combo box','Check button'])
    self.cBox1.grid(row=0,column=1,sticky=tki.W)
    self.cBox1Var.set('Text entry')
    self.cBox1Var.bind("<<ComboboxSelected>>", lambda event, count=count: self.EnableDisableParamFields(event, count))

    self.cBox2Var=tki.StringVar()
    self.cBox2=ttk.Combobox(self.mframe, width=16, textvariable=self.cBox2Var, state='readonly',values=['String','Integer','Float'])
    self.cBox2.grid(row=0,column=2,sticky=tki.W)
    self.cBox2Var.set('String')

...

def EnableDisableParamFields(self, event, count):
    if self.cBox1Var.get()=='Combo box':  #disable 'Entry format combo box'
        #disable "self.cBox2"
    else:
        #enable "self.cBox2"

提前感谢。

编辑!!!

经过坚持不懈的努力,找到了答案,而且它非常简单。 对于那些可能感兴趣的人,可以在这里找到解决方案:http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_combobox.htm

"state='disabled'、'readonly'或'normal'"


2
BWT:您甚至可以使用.grid_forget()隐藏小部件。 - furas
1个回答

14
您想使用 state='disabled'Combobox 选项。 state 有三个选项,如下所示:
  • state='normal' 是完全功能的 Combobox
  • state='readonly' 是具有值但无法直接更改的 Combobox
  • state='disabled' 是无法与之交互的 Combobox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接