如何清除tkinter中Entry小部件的先前输入?

时间:2017-06-05 00:29:46

标签: python tkinter tkinter-entry

当我在a小部件中输入输入值Entry并点击Click Me时。此按钮调用更新takes_input()小部件的Text方法。看起来像这样:

enter image description here

再次输入下一个输入值b,它附加了之前的值,如下所示:

enter image description here

我的代码:

from tkinter import *       

# def func for button callback


def takes_input():
    textBox.insert(INSERT, inputBox_val.get())

# object for windows
window = Tk()
window.title("Learning GUI")

inputBox_val = StringVar()

# create button on windows
myBtn = Button(window, text="Click Me", command = takes_input)         

myBtn.grid(row = 0, column = 0)


# input filed
inputBox = Entry(window, textvariable = inputBox_val)
inputBox.grid(row = 0, column = 1)


# textBox filed
textBox = Text(window, height = 1, width = 20)
textBox.grid(row = 0, column =2)

window.mainloop()          

问题是:

当我再次点击Entry按钮时,我想要清除Click Me小部件的先前输入。

我是怎么做到的?

1 个答案:

答案 0 :(得分:1)

您必须使用delete('1.0', END)清除文字。在你的情况下:

def takes_input():
    textBox.delete('1.0', END)
    textBox.insert(INSERT, inputBox_val.get())