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
self.outputbox.delete("1.0", self.tk.END)
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
Use:
def delete_whole_word(event):
text.delete("insert-1c wordstart", "insert")
return "break"
Related Topics
How to Count the Number of Messages
How to Reset Anaconda Root Environment
How to Normalize a Numpy Array to Within a Certain Range
How to Get Maximum Length of Each Column in the Data Frame Using Pandas Python
How to Convert Data from Txt Files to Excel Files Using Python
How to Concatenate/Append Multiple Spark Dataframes Column Wise in Pyspark
Calculating the Area Under a Curve Given a Set of Coordinates, Without Knowing the Function
How to Find Duration Between Two Time Difference in Python Dataframe
How to Detect and Remove Outliers from Each Column of Pandas Dataframe At One Go
How to Sort the Letters in a String Alphabetically in Python
How to Mention a User in Discord.Py
How to Remove Square Brackets from List in Python
How to Continue a Loop After Catching Exception in Try ... Except
Delete Rows Containing Numeric Values in Strings from Pandas Dataframe
Use Variable as Key Name in Python Dictionary
Most Pythonic Way to Kill a Thread After Some Period of Time