How to clear the contents of a Tkinter Text widget?

The Tkinter Text widget is used to create multi-line text areas in GUI applications. When you need to clear all content from a Text widget, you can use the delete() method with appropriate start and end positions.

Syntax

To clear the entire contents of a Text widget ?

text_widget.delete("1.0", "end")

Where:

  • "1.0" - Start position (line 1, character 0)
  • "end" - End of the text content

Example

Here's a complete example showing how to create a Text widget with a clear button ?

# Import the tkinter library
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Set the geometry
win.geometry("600x250")
win.title("Text Widget Clear Example")

# Define a function to clear the input text
def clear_text_input():
    my_text.delete("1.0", "end")

# Create a text widget
my_text = Text(win, height=10, width=50)
my_text.pack(pady=10)

# Add some initial text for demonstration
my_text.insert("1.0", "Type your text here...\nClick Clear to remove all content.")

# Create a Clear button
btn = Button(win, height=1, width=10, text="Clear", command=clear_text_input)
btn.pack()

# Display the window
win.mainloop()

How It Works

The delete("1.0", "end") method works by:

  • Starting from position "1.0" (first line, first character)
  • Removing all characters up to "end" (the last character)
  • Leaving the Text widget completely empty

Alternative Methods

Using END Constant

You can also use the END constant instead of the string "end" ?

from tkinter import *

def clear_text():
    text_widget.delete("1.0", END)

root = Tk()
text_widget = Text(root)
text_widget.pack()

Button(root, text="Clear", command=clear_text).pack()
root.mainloop()

Clearing and Adding New Content

You can combine clearing with inserting new content ?

def reset_text():
    my_text.delete("1.0", "end")
    my_text.insert("1.0", "Fresh content here!")

Key Points

  • Position "1.0" refers to line 1, character 0 (beginning of text)
  • The "end" parameter removes everything to the last character
  • This method preserves the Text widget structure, only clearing content
  • You can use either "end" string or END constant

Conclusion

Use text_widget.delete("1.0", "end") to clear all content from a Tkinter Text widget. This method is efficient and preserves the widget's functionality for future text input.

Updated on: 2026-03-25T18:25:17+05:30

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements