禁用和重新启用小部件时如何删除条目文本

How to delete Entry text when disabling and re-enabling widget

有什么方法可以在输入小部件的状态被禁用并重新启用时删除用户在输入小部件中的输入?用户输入保持原样,我希望无需添加按钮事件。

 from Tkinter import *

 class Interface():
     def __init__(self, window):
        frame = Frame(window)
        frame.pack()

        self.hopLabel = Label(frame, text="Number:", anchor=E)
        self.hopLabel.grid(row=0, column=0, sticky=EW)

        options = range(0,6)
        options.append("Other")

        self.variable = StringVar(frame)
        self.variable.set(options[0])
        self.options = OptionMenu(frame, self.variable, *options, command=self.this)
        self.options.grid(row=0, column=2, sticky=EW)

        self.button = Button(frame,text = "Print Value",width=20,command = self.printit(self.variable.get()))
        self.button.grid(row=1)

        self.otherEntry = Entry(frame, state=DISABLED)
        self.otherEntry.grid(row=0, column=1, sticky=EW)

    def this(self, value):
        if value == "Other":
            self.otherEntry.config(state=NORMAL)
        else:
            self.otherEntry.config(state=DISABLED)

    def printit(self,value):
        print value

if __name__ == "__main__":
    root = Tk()
    app = Interface(root)
    root.mainloop()

为了节省space,我没有添加打印"Other"选项值的函数。我的问题又是:当小部件的状态从 DISABLED 变为 NORMAL 时,是否无需按按钮就可以删除输入框中的值?

要在禁用状态下删除条目小部件中的文本,您只需先将状态设置为正常,然后调用delete方法:

def this(self, value):
    if value == "Other":
        self.otherEntry.config(state=NORMAL)
        self.otherEntry.delete(0, "end")
    ...