How to Delete Tkinter Text Box's Contents?


Tkinter provides many functions and modules through which we can create fully featured applications with buttons, dialogue boxes, widgets, and many more.

To create a text widget, we can use the tkinter entry widget function which is basically a constructor and it takes the window or frame of the tkinter. Further, we can delete the content of this text widget using the built-in method delete(first, last=None) which basically takes a range within the textbox.

In this example, we will create a Delete Button which basically deletes all the contents from the given text box.

Example

from tkinter import *

win= Tk()
win.geometry("600x300")

label= Label(win, text= "Write something ??", font= ('Helvetica', 25))
label.pack(pady=20)

#Create a Text Widget
text= Text(win, height=10)
text.pack()

def delete():
   text.delete("1.0","end")

#Create a Delete Button to remove the Text from the text-widget

b1= Button(win, text= "Delete",command= delete)
b1.pack(pady=10)
win.mainloop()

Output

Running the above code will create a text widget and a delete button which can be used to delete the contents written in the text box.

Type something inside the textbox, and then, click the “Delete” button. It will erase the content inside the textbox.

Updated on: 04-Mar-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements