How to clear the Entry widget after a button is pressed in Tkinter? How to clear the Entry widget after a button is pressed in Tkinter? tkinter tkinter

How to clear the Entry widget after a button is pressed in Tkinter?


After poking around a bit through the Introduction to Tkinter, I came up with the code below, which doesn't do anything except display a text field and clear it when the "Clear text" button is pushed:

import tkinter as tkclass App(tk.Frame):    def __init__(self, master):        tk.Frame.__init__(self, master, height=42, width=42)        self.entry = tk.Entry(self)        self.entry.focus()        self.entry.pack()        self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)        self.clear_button.pack()    def clear_text(self):        self.entry.delete(0, 'end')def main():    root = tk.Tk()    App(root).pack(expand=True, fill='both')    root.mainloop()if __name__ == "__main__":    main()


I'm unclear about your question. From http://effbot.org/tkinterbook/entry.htm#patterns, itseems you just need to do an assignment after you called the delete.To add entry text to the widget, use the insert method. To replace the current text, you can call delete before you insert the new text.

e = Entry(master)e.pack()e.delete(0, END)e.insert(0, "")

Could you post a bit more code?


real gets the value ent.get() which is just a string. It has no idea where it came from, and no way to affect the widget.

Instead of real.delete(), call .delete() on the entry widget itself:

def res(ent, real, secret):    if secret == eval(real):        showinfo(message='that is right!')    ent.delete(0, END)def guess():    ...    btn = Button(ge, text="Enter", command=lambda: res(ent, ent.get(), secret))