- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 change the color of certain words in a Tkinter text widget?
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.
Example
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()
Output
Running the above code will display a window with a text containing the string “Hello World” where “World” contains some specific color.
- Related Articles
- Dynamically change the widget background color in Tkinter
- How to change text cursor color in Tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- How to highlight text in a tkinter Text widget?
- Change the focus from one Text widget to another in Tkinter
- Python Tkinter – How do I change the text size in a label widget?
- How to clear the contents of a Tkinter Text widget?
- How to get the current length of the Text in a Tkinter Text widget?
- How to create hyperlink in a Tkinter Text widget?
- How do I center the text in a Tkinter Text widget?
- How to highlight the current line of a Text widget in Tkinter?
- Get the text of a button widget in Tkinter
- How to set the border color of certain Tkinter widgets?
- How to take input in a text widget and display the text in tkinter?
- How to attach a Scrollbar to a Text widget in Tkinter?

Advertisements