Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the color of certain words in a Tkinter text widget?
Tkinter text widgets allow you to create multiline text displays with rich formatting. You can change the color of specific words using the tag_add() and tag_config() methods.
The tag_add(tag_name, start, end) method selects a range of text to format, while tag_config(tag_name, properties) applies styling like color, font, and background.
Basic Example
Here's how to change the color of specific words in a text widget ?
import tkinter as tk
# Create main window
root = tk.Tk()
root.geometry("600x250")
root.title("Colored Text Widget")
# Create text widget
text_widget = tk.Text(root)
text_widget.insert(tk.INSERT, "Hello World!\n")
text_widget.insert(tk.END, "This is a New Line")
text_widget.pack(fill=tk.BOTH, expand=True)
# Configure tag with red color
text_widget.tag_config("red_text", foreground="red")
# Apply tag to "World" (position 1.6 to 1.12)
text_widget.tag_add("red_text", "1.6", "1.12")
root.mainloop()
Multiple Colors Example
You can create multiple tags for different colors ?
import tkinter as tk
root = tk.Tk()
root.geometry("700x300")
root.title("Multiple Colored Words")
text_widget = tk.Text(root, font=("Arial", 12))
text_widget.insert(tk.END, "Python is awesome!\n")
text_widget.insert(tk.END, "Tkinter makes GUI development easy.\n")
text_widget.insert(tk.END, "Colors make text more appealing.")
text_widget.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Configure multiple color tags
text_widget.tag_config("blue", foreground="blue", font=("Arial", 12, "bold"))
text_widget.tag_config("green", foreground="green", font=("Arial", 12, "italic"))
text_widget.tag_config("red", foreground="red", background="yellow")
# Apply colors to different words
text_widget.tag_add("blue", "1.0", "1.6") # "Python"
text_widget.tag_add("green", "2.0", "2.7") # "Tkinter"
text_widget.tag_add("red", "3.0", "3.6") # "Colors"
root.mainloop()
Understanding Text Positions
Text positions in Tkinter use the format "line.column" where:
- Line: Starts from 1 (first line)
- Column: Starts from 0 (first character)
- Example: "1.6" means line 1, character 6
Dynamic Color Changes
You can also change colors dynamically using functions ?
import tkinter as tk
def highlight_word():
# Clear existing tags
text_widget.tag_remove("highlight", "1.0", tk.END)
# Get word to highlight
word = entry.get()
if word:
start_pos = "1.0"
while True:
start_pos = text_widget.search(word, start_pos, tk.END)
if not start_pos:
break
end_pos = f"{start_pos}+{len(word)}c"
text_widget.tag_add("highlight", start_pos, end_pos)
start_pos = end_pos
root = tk.Tk()
root.geometry("600x350")
root.title("Dynamic Text Highlighting")
# Entry for word input
frame = tk.Frame(root)
frame.pack(pady=10)
tk.Label(frame, text="Highlight word:").pack(side=tk.LEFT)
entry = tk.Entry(frame)
entry.pack(side=tk.LEFT, padx=5)
tk.Button(frame, text="Highlight", command=highlight_word).pack(side=tk.LEFT)
# Text widget
text_widget = tk.Text(root, font=("Arial", 11))
text_widget.insert(tk.END, "Python programming is fun.\n")
text_widget.insert(tk.END, "Python is powerful and easy to learn.\n")
text_widget.insert(tk.END, "Many developers love Python.")
text_widget.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Configure highlight tag
text_widget.tag_config("highlight", background="yellow", foreground="blue")
root.mainloop()
Tag Configuration Options
| Property | Description | Example |
|---|---|---|
foreground |
Text color | "red", "#FF0000" |
background |
Background color | "yellow", "#FFFF00" |
font |
Font family, size, style | ("Arial", 12, "bold") |
underline |
Underline text | True/False |
Conclusion
Use tag_add() to select text ranges and tag_config() to apply colors and formatting. Text positions follow the "line.column" format starting from "1.0". Tags provide powerful text styling capabilities for creating rich text interfaces.
