Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 Text widget which is basically a constructor that takes the window or frame of the tkinter. We can delete the content of this text widget using the built-in method delete(first, last=None) which takes a range within the textbox.
Syntax
text_widget.delete(start, end)
Parameters:
- start ? Starting position (e.g., "1.0" for beginning)
- end ? Ending position (e.g., "end" for end of text)
Example
In this example, we will create a Delete Button which deletes all the contents from the given text box ?
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.

Deleting Specific Text Range
You can also delete specific portions of text by specifying different start and end positions ?
from tkinter import *
win = Tk()
win.geometry("400x300")
text = Text(win, height=8)
text.pack(pady=20)
# Insert some sample text
text.insert("1.0", "Line 1\nLine 2\nLine 3\nLine 4")
def delete_first_line():
text.delete("1.0", "2.0")
def delete_all():
text.delete("1.0", "end")
Button(win, text="Delete First Line", command=delete_first_line).pack(pady=5)
Button(win, text="Delete All", command=delete_all).pack(pady=5)
win.mainloop()
Key Points
-
"1.0"represents the first line, first character -
"end"represents the end of all text -
"2.0"would be the beginning of the second line - The
delete()method removes text between the specified positions
Conclusion
Use text.delete("1.0", "end") to clear all text content from a Tkinter Text widget. You can also delete specific ranges by adjusting the start and end parameters.
