HOWTO · Tkinter
How to Delete Tkinter Text Box's Contents
Learn how to clear a Tkinter Text widget with delete, remove a range or selection, and check its contents before clearing it.
On this page
To clear a Tkinter Text widget, call its delete method with the first text index and the end index: text_box.delete("1.0", "end"). The first index, 1.0, is the start of the first line. Tkinter keeps a required trailing newline internally, so deleting through end removes all entered text.
Clear all text from a Tkinter Text widget
Use text_box.delete("1.0", "end") to remove the entered text. The 1.0 index marks the start of the first line, and end reaches Tkinter’s trailing newline sentinel. The following headless example uses a small stand-in to demonstrate the call and resulting content without opening a window:
class TextBox:
def __init__(self, content):
self.content = content
def delete(self, start, end):
if start == "1.0" and end == "end":
self.content = ""
text_box = TextBox("Draft text")
text_box.delete("1.0", "end")
print("Cleared" if text_box.content == "" else "Still has text")
Cleared
In a GUI, set text_box to a tk.Text widget and call the same delete("1.0", "end") method from a button callback. That GUI version needs a graphical display, so it is not shown as an executed example here.
Delete a range or one character
Text.delete(index1, index2=None) removes text starting at index1 and, when supplied, continuing up to but not including index2. Text indices use the form line.character; lines start at 1 and character positions start at 0. For example, text_box.delete("2.0", "3.0") removes the text on line 2 up to the start of line 3. With one index, such as text_box.delete("2.0"), Tkinter deletes the character at that position. The end index refers to the position after the widget’s trailing newline, which is why it clears all entered text.
Delete selected text safely
tk.SEL_FIRST and tk.SEL_LAST identify the selected range, but using them when no selection exists can raise tk.TclError. Check for the sel tag before deleting: if text_box.tag_ranges("sel"): then call text_box.delete(tk.SEL_FIRST, tk.SEL_LAST). The selection’s ending index is exclusive, so this removes the selected text without deleting the character immediately after it. When the sel tag has no ranges, the guarded block does nothing.
Clear text conditionally
To clear only when the widget contains user-entered characters, retrieve the content up to end-1c, which excludes Tkinter’s trailing newline. An empty string means there are no entered characters; spaces and tabs still count as content. For example, if text_box.get("1.0", "end-1c"): is true for any nonempty content, including whitespace. Put text_box.delete("1.0", "end") in that branch to clear it. If whitespace-only content should count as blank in your application, test text_box.get("1.0", "end-1c").strip() instead; that treats spaces and tabs as blank too.