How to Clear/Delete the Contents of a Tkinter Text Widget

How to clear/delete the contents of a Tkinter Text widget?

I checked on my side by just adding '1.0' and it start working

tex.delete('1.0', END)

you can also try this

TKinter - Clear text widget

Do this

self.outputbox.delete("1.0", self.END)

it will clear all the text widget

And if you imported tkinter as tk do this


Remove all tags of a tkinter text widget

To remove any single tag, use tag_remove, giving it the tag name. To remove from the entire document use the index "1.0" and "end"

text.tag_remove("the_tag", "1.0", "end")

To remove all tags, iterate over a list of all tags. You can get a list of all the tags with tag_names(). The list will include all of your custom tags along with the tag "sel" which is used to manage the selection.

for tag in text.tag_names():
text.tag_remove(tag, "1.0", "end")

How to erase everything from the tkinter text widget?

Most likely your problem is that your binding is happening before the newline is inserted. You delete everything, but then the newline is inserted. This is due to the nature of how the text widget works -- widget bindings happen before class bindings, and class bindings are where user input is actually inserted into the widget.

The solution is likely to adjust your bindings to happen after the class bindings (for example, by binding to <KeyRelease> or adjusting the bindtags). Without seeing how you are doing the binding, though, it's impossible for me to say for sure that this is your problem.

Another problem is that when you get the text (with Tex2.get("1.0",END)), you are possibly getting more text than you expect. The tkinter text widget guarantees that there is always a newline following the last character in the widget. To get just what the user entered without this newline, use Tex2.get("1.0","end-1c"). Optionally, you may want to strip all trailing whitespace from what is in the text widget before sending it to the client.

Deleting a whole word in tkinter text widget


def delete_whole_word(event):
text.delete("insert-1c wordstart", "insert")
return "break"

Related Topics

Leave a reply