如何在Tkinter中禁用组合框?

14 投票
1 回答
16744 浏览
提问于 2025-04-18 12:20

基本上,我想根据另一个下拉框的值来禁用某个下拉框。 我找不到这个问题的答案,可能是因为这样做的情况很少见。

我的代码大概是这样的……

    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' ”

1 个回答

15

你想使用 Comboboxstate='disabled' 选项。

关于 state,有三种选择,分别是:

  • state='normal',这是完全可以使用的 Combobox
  • state='readonly',这是一个有值的 Combobox,但是不能直接修改这个值。
  • state='disabled',这是一个无法进行任何操作的 Combobox

撰写回答