How to erase everything from a Tkinter text widget?


Tkinter Text widget is more than just a multiline Entry widget. It supports the implementation of multicolored text, hyperlink text, and many more.

Let us suppose a text widget has been created in an application. Now, to clear the Text widget, we can use the delete("1.0", END) method. It can be invoked in a callback function or event which can be triggered through an object of the Button Class.

Example

# Import the required libraries
from tkinter import *

# Create an instance of Tkinter Frame
win = Tk()

# Set the geometry
win.geometry("750x250")

# Define a function to clear the text widget
def clear():
   text.delete('1.0', END)

# Create a Text Widget
text = Text(win, width=50, height=10)
text.insert("1.0", "This is my Text Widget")
text.pack(padx=5, pady=5)

# Create a Button to delete all the text
Button(win, text="Clear All", command=clear, font="aerial 12 bold").pack(padx=5, pady=5)

win.mainloop()

Output

Running the above code will display a text widget and a button to clear the Text Widget.

Now, click the "Clear All" button to clear the text inside the text widget.

Updated on: 25-May-2021

665 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements