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

时间:2016-01-20 22:49:13

标签: python-2.7 tkinter optionmenu tkinter-entry

当禁用并重新启用状态时,我是否可以删除条目窗口小部件中的用户输入?用户输入保持原样,我希望它不需要添加按钮事件。

 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()

为了节省空间,我没有添加打印"其他"的值的功能。选项。我的问题是:当小部件的状态从DISABLED变为NORMAL而不必按下按钮时,是否还要删除输入框中的值?

1 个答案:

答案 0 :(得分:0)

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

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