- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to erase everything from the Tkinter text widget?
Tkinter Text widget accepts and supports multiline user input. We can specify other properties of the Text Widget such as the width, height, background, border-width, and other properties as well.
Let us suppose that we want to erase all the content in the given Text widget; then, we can use the delete("1.0", END) function.
In this example, we will insert some random text using random module in Python and erase using the delete() method.
#Import the required Libraries from tkinter import * from tkinter import ttk import random #Create an instance of Tkinter frame win = Tk() #Set the geometry of Tkinter Frame win.geometry("750x250") #Define functions to insert/erase the text def insert_text(): text.insert(INSERT,chr(random.randint(ord('a'),ord('z')))) def erase_text(): text.delete("1.0",END) #Create a Text widget text= Text(win, width=50, height= 5) text.focus_set() text.pack() #Add a bottom widgets button1= ttk.Button(win, text= "Insert",command= insert_text) button1.pack(side=TOP) button2= ttk.Button(win, text= "Erase",command= erase_text) button2.pack(side=TOP) #Create a Button widget win.mainloop()
Run the above code to display a window that contains a text widget and buttons to erase all the content of it.
Now, click the "Insert" Button to insert some random character in the Text Box. Once the characters are inserted in the text box we can erase all the content by clicking on the button "Erase".
- How to erase everything from a Tkinter text widget?
- How to get the input from the Tkinter Text Widget?
- How to highlight text in a tkinter Text widget?
- How to make the Tkinter text widget read only?
- Change the focus from one Text widget to another in Tkinter
- How to clear the contents of a Tkinter Text widget?
- How do I center the text in a Tkinter Text widget?
- Underline Text in Tkinter Label widget
- How to get the current length of the Text in a Tkinter Text widget?
- How to create hyperlink in a Tkinter Text widget?
- How to clear Text widget content while clicking on the Entry widget itself in Tkinter?
- How to take input in a text widget and display the text in tkinter?
- Add advanced features to a tkinter Text widget
- Printing a list to a Tkinter Text widget
- How to set default text for a Tkinter Entry widget?