- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 highlight text in a tkinter Text widget?
Tkinter Text widget is used to create text fields that accept multiline user inputs. Let us suppose that in a text widget, we want to highlight some text. In order to highlight a specific text written in the text widget, tkinter provides the tag_add(tag, i,j) method. It adds tags to the specific text by defining the indexes, i and j.
In this example, we will create a window application that contains some text and a button which can be triggered to highlight the text.
#Import tkinter library from tkinter import * #Create an instance of tkinter frame win= Tk() win.geometry("750x450") #Define a function to highlight the text def add_highlighter(): text.tag_add("start", "1.11","1.17") text.tag_config("start", background= "black", foreground= "white") #Create a Tex Field text= Text(win); text.insert(INSERT, "Hey there! Howdy?") text.pack() #Create a Button to highlight text Button(win, text= "Highlight", command= add_highlighter).pack() win.mainloop()
Running the above code will display a window that contain a button and a text in it.
Now, click the "Highlight" button to highlight the text "Howdy?"
- Related Articles
- How to highlight the current line of a Text widget in Tkinter?
- How do I center the text in a Tkinter Text widget?
- How to create hyperlink in a Tkinter Text widget?
- How to take input in a text widget and display the text in tkinter?
- How to erase everything from a Tkinter text widget?
- How to attach a vertical scrollbar in Tkinter text widget?
- How to get the current length of the Text in a Tkinter Text widget?
- Printing a list to a Tkinter Text widget
- How to attach a Scrollbar to a Text widget in Tkinter?
- Underline Text in Tkinter Label widget
- Add advanced features to a tkinter Text widget
- How to insert a temporary text in a tkinter Entry widget?
- How to set default text for a Tkinter Entry widget?
- How to clear the contents of a Tkinter Text widget?
- Select all text in a Text widget using Python 3 with tkinter