How to change the color of certain words in a Tkinter text widget?

TkinterPythonServer Side ProgrammingProgramming

Tkinter text widgets are used to create and display multiline text Input. It provides several functions and methods that are generally used to configure a text widget.

Let us suppose we want to change the color of certain words in a text widget, then we can use the tag_add(tag name, range) method which selects the word we want to format. Once the word has been selected, we can change its color, background color, and other properties using the tag_config(properties) method.


In this example, we will configure the color of a selected word in the text widget.

#Import required libraries
from tkinter import *

#Create an instance of tkinter window
win =Tk()

#Define the geometry of the window

#Create a text widget
text= Text(win)
text.insert(INSERT, "Hello World!\n")
text.insert(END, "This is a New Line")


#Configure the text widget with certain color
text.tag_config("start", foreground="red")
text.tag_add("start", "1.6", "1.12")



Running the above code will display a window with a text containing the string “Hello World” where “World” contains some specific color.

Updated on 26-Mar-2021 11:16:15