Python:将操作绑定到Checkbutton

时间:2015-06-02 23:13:18

标签: python tkinter

从我在互联网教程中找到的内容来看,Checkbutton有一个 command 选项。所以,这是不正确的,或者我对如何使用该选项感到难过。我要做的是在未选中Checkbutton时将文本输入框设置为灰色(disabled),并在选中Checkbutton时启用文本输入框。

我的代码:     class OutputSettings:       def init (自我,主):

    frame = LabelFrame(master, text = 'Debug')
    frame.grid(row=2, column=0, columnspan = 2)

    self.make_txt = BooleanVar()
    self.make_txt.set(True)

    self.create_a_file = Checkbutton(frame, text = 'Create Text File')
    self.create_a_file.config(variable = self.make_txt, 
                              command = self.graytextbox())
    self.create_a_file.pack()


    Label(frame, 
          text="Text File \nSave Location:", 
          font = "Verdana 8").pack()

    self.file_location = StringVar()
    self.file_location =   Entry (frame, width=30, textvariable = self.file_location)
    self.file_location.pack()
    self.file_location.insert(0, 'C:\Temp\ETHresults.txt')


  def graytextbox(self):

    if self.make_txt == True:
        self.file_location.config(state=DISABLED)
        # I've also tried self.file_location.state(['DISABLED'])
    else:
        self.file_location.config(state=NORMAL)
        # I've also tried self.file_location.state(['NORMAL'])

代码不仅不能控制按钮的正常/禁用功能,而且还得到AttributeError: instance has no attribute 'file_location'

0 个答案:

没有答案