2017-06-05 1730 views
0

当我在Entry小部件中输入输入值a并点击Click Me。该按钮调用takes_input()方法,该方法更新Text小部件。看起来是这样的:如何清除tkinter中来自Entry小部件的以前输入?

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按钮时。

我该怎么做?

+0

你需要做'inputBox.delete(0,'end')'清除整个输入框。 –

+0

@ChristianDean上面的问题解决了我的问题,我有搜索但没有找到,现在我应该删除它还是投票关闭? –

+2

这很好。把它留下吧。它可以作为答案的指针。只要确保你在将来做更多的研究;-) –

回答

1

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

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