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 win.geometry("600x250") #Create a text widget text= Text(win) text.insert(INSERT, "Hello World!\n") text.insert(END, "This is a New Line") text.pack(fill=BOTH) #Configure the text widget with certain color text.tag_config("start", foreground="red") text.tag_add("start", "1.6", "1.12") win.mainloop()
Running the above code will display a window with a text containing the string “Hello World” where “World” contains some specific color.