- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- 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.
Example
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()
Output
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?"
Advertisements