Change the color of certain words in the tkinter text widget
Python has various options for Graphical User Interface (GUI) development. One of the options is Tkinter. Tkinter and Python together provide a faster way for GUI development. The Tk GUI toolkit provides an object-oriented interface.
For creating GUI applications using Tkinter we have to follow a few steps –
- Import Tkinter module.
- Create the main window.
- Add various widgets to the GUI application as per requirements.
- Main event loop for every trigger by the user for performing specific actions.
Text widgets have advanced options for editing a text with multiple lines and format the display settings of that text example font, text color, background color. We can also use tabs and marks for locating and editing sections of data. We can also use images in the text and insert borders as well. And everything can be formatted as per the requirements.
Syntax : Text ( master, option, … )
Parameters : master represents parent window and option represents various widget options. They can be given as key-value pairs separated by commas.
Return : Return a Text object.
Example 1 : In first example we will add a tag to a section of text by specifying the indices and highlight the selected text. Here, we are using tag_add and tag_config.
Example 2 : In this example, user can highlight text as per their wish by selecting the text to be highlighted. Here, we are using tag_configure and tag_add.
Before selecting the text and hitting the highlight button :
After selecting the text and hitting the highlight button :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course