To clear a Tkinter Entry widget, call its delete() method with the first and last positions to remove. In most cases, the simplest solution is entry.delete(0, tk.END), which removes all text from the field. This works with both the standard tkinter.Entry widget and ttk.Entry, and it is commonly used after a form is submitted or when a user clicks a Clear button.
Below, you’ll find the basic command, complete examples, and tips for clearing one or several Entry fields safely.
The Basic Way to Clear a Tkinter Entry
Every Tkinter Entry widget provides a delete(first, last=None) method. The first argument is the index where deletion starts, and the optional second argument is the index where it ends. To remove all the text, start at position 0 and use Tkinter’s END constant for the final position:
entry.delete(0, tk.END)
Here, 0 means the beginning of the text, while tk.END refers to the position immediately after the last character. Using tk.END is more reliable than guessing the text’s length, because it works no matter how many characters the user entered.
Make sure that entry is the name of your Entry widget and that you have imported Tkinter. For example:
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
entry.delete(0, tk.END)
root.mainloop()
This example clears the Entry as soon as the program runs, so the field will already be empty when the window appears. Usually, you’ll call the same method in response to an event, such as a button click or a successful form submission.
Clear an Entry with a Button
To let someone clear a field on demand, connect a button’s command option to a function. The function can call delete(0, tk.END) on the Entry widget:
import tkinter as tk
def clear_entry():
entry.delete(0, tk.END)
root = tk.Tk()
root.title("Clear an Entry")
entry = tk.Entry(root)
entry.pack(padx=10, pady=10)
clear_button = tk.Button(
root,
text="Clear",
command=clear_entry
)
clear_button.pack(pady=5)
root.mainloop()
In this example, clear_entry is passed to the button without parentheses. Tkinter calls the function when the button is clicked. Writing command=clear_entry() would call the function immediately while the interface is being created, rather than waiting for a click.
You can also use a lambda when the action is short:
clear_button = tk.Button(
root,
text="Clear",
command=lambda: entry.delete(0, tk.END)
)
A named function is often easier to read and extend, particularly if clearing the Entry is only one part of the button’s job. A lambda is convenient when the button needs to perform a single, simple action.
Clear the Entry After Submitting a Form
A common pattern is to read the Entry’s contents, process the value, and then clear the field. Use get() to retrieve the text. Once you have saved or used that value, call delete(0, tk.END):
import tkinter as tk
def submit():
value = entry.get()
if value.strip():
print("Submitted:", value)
entry.delete(0, tk.END)
root = tk.Tk()
root.title("Submit a Value")
entry = tk.Entry(root)
entry.pack(padx=10, pady=10)
submit_button = tk.Button(root, text="Submit", command=submit)
submit_button.pack()
root.mainloop()
The strip() check ignores input that contains only spaces. Because the text is stored in value before the widget is cleared, the program can still use it after the Entry becomes empty. In a real application, you might replace print() with code that saves a record, performs a search, or updates another part of the interface.
If you clear the Entry before calling get(), the retrieved value will be empty. The order of these operations matters: read and validate first, then clear when you’re ready.
Clear Multiple Entry Widgets
If a form has several fields, clear each one by calling delete(0, tk.END) on each widget. You can put the calls in a helper function and connect that function to a Reset or Clear Form button:
def clear_form():
name_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
search_entry.delete(0, tk.END)
For a larger form, keep the Entry widgets in a list and loop through them:
entries = [name_entry, email_entry, search_entry]
def clear_form():
for widget in entries:
widget.delete(0, tk.END)
This approach can make the code easier to maintain when a form contains many fields. Add each Entry widget to the list when you create it, then reuse the same loop whenever the form needs to be reset. If the form also contains checkboxes, text areas, or other controls, handle those widgets with their own appropriate methods.
Clear an Entry That Uses StringVar
An Entry can display a value stored in a Tkinter variable, such as StringVar. You can still clear it with delete(0, tk.END). Alternatively, set the associated variable to an empty string. Both approaches update the displayed field:
import tkinter as tk
root = tk.Tk()
text_var = tk.StringVar()
entry = tk.Entry(root, textvariable=text_var)
entry.pack()
# Either option clears the field:
entry.delete(0, tk.END)
# text_var.set("")
root.mainloop()
Using text_var.set("") can be useful when your application already manages the value through the variable—for example, when several widgets share or observe that variable. Use delete() when you want to act directly on the Entry. In either case, keep the variable and widget state in mind if your application has validation or trace callbacks that react to value changes.
Clear Only Part of the Text
You don’t have to delete the entire contents. The delete() method accepts positions, so you can remove a range of characters. For example, this removes the first three characters:
entry.delete(0, 3)
Entry indices begin at zero. The end position is treated as the boundary of the deletion range, so the characters at positions 0, 1, and 2 are removed in this example. You can also remove text from a chosen position to the end:
entry.delete(4, tk.END)
To delete only the text currently selected by the user, check the selection range and remove it:
try:
start = entry.index(tk.SEL_FIRST)
end = entry.index(tk.SEL_LAST)
entry.delete(start, end)
except tk.TclError:
# No text is selected.
pass
The selection constants raise a TclError if there is no selection, which is why the example uses try and except. For the common task of emptying the entire field, you don’t need this extra logic—use entry.delete(0, tk.END).
Works with ttk.Entry Too
Themed Entry widgets from tkinter.ttk use the same delete() method. The main difference is how you create the widget; the command for clearing it does not change:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
entry = ttk.Entry(root)
entry.pack(padx=10, pady=10)
clear_button = ttk.Button(
root,
text="Clear",
command=lambda: entry.delete(0, tk.END)
)
clear_button.pack()
root.mainloop()
If your code uses import tkinter as tk, write tk.END. If instead you import Tkinter names directly with from tkinter import * , you can write END, but explicit imports are generally clearer and help avoid naming conflicts.
What If the Entry Is Disabled or Read-Only?
An Entry in the disabled state cannot be edited normally. If you need to clear it, temporarily change its state, delete the text, and then restore its original state:
entry.configure(state="normal")
entry.delete(0, tk.END)
entry.configure(state="disabled")
For a ttk.Entry in the readonly state, the field is intended to prevent direct editing. If its value is linked to a StringVar, setting the variable to an empty string may be the more suitable option:
text_var.set("")
Whether this works as expected can depend on how the widget and application are configured. If you need users to clear the value themselves, consider providing a separate Clear button and defining how that button should interact with the widget’s state.
Common Mistakes to Avoid
-
Using the wrong widget name: Call
delete()on the Entry you created, not on the root window or a different widget. -
Forgetting the index range: Use
entry.delete(0, tk.END)to remove all text. Callingdelete()without the required starting index will raise an error. -
Calling the button function too early: Set
command=clear_entry, notcommand=clear_entry(). -
Using the wrong constant: With the common
import tkinter as tkstyle, usetk.END, not an undefinedEND. -
Clearing before reading the value: Call
get()and store the result before deleting the Entry’s contents. - Assuming a disabled field can be edited: Change its state first or update its linked variable as appropriate.
If the Entry appears not to clear, check that the event handler is actually running, that the widget reference points to the field you can see, and that the widget isn’t disabled or controlled by application logic that immediately puts the old value back.
Conclusion
The standard way to clear a Tkinter Entry is entry.delete(0, tk.END). Call it directly, place it in a function connected to a button, or run it after you have processed a submitted value. For several fields, loop over the Entry widgets; for an Entry linked to a StringVar, you can also set that variable to an empty string. Remember to read any value you need before clearing it, and check the widget’s state if deletion doesn’t work.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.