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
Adding coloured text to selected text in Tkinter
If we want to implement a text editor in an application that can accept multiline user input, then we can use the Tkinter Text widget. The Text widget in Tkinter is generally used to create a text editor for an application, where we can write text and perform operations like selecting, editing and creating a specific text in the application.
If you want to highlight a text and provide a color to the highlighted text, then you can use the tag_add("start", "first", "second") method. The tag_add() method takes two arguments for selecting the specified text from the text widget. You can give a background color to the highlighted text by configuring the tags using tag_configure() method.
Basic Text Highlighting Example
Here's how to add colored text to selected portions in a Tkinter Text widget ?
# Import the required library
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
win.title("Colored Text Example")
# Create a new frame
frame = Frame(win)
# Add a text widget
text = Text(frame)
# Insert a new text
text.insert(INSERT, "Hello, Welcome to TutorialsPoint.com")
text.pack()
# Add a tag to the specified text
text.tag_add("start", "1.8", "1.35")
text.tag_configure("start", background="black", foreground="yellow")
frame.pack()
win.mainloop()
Understanding Text Indexing
The indexing format in Tkinter Text widget is "line.column". In the example above:
-
"1.8"means line 1, character position 8 (starting from 0) -
"1.35"means line 1, character position 35 - This selects the word "Welcome to TutorialsPoint.com" for highlighting
Multiple Color Highlighting
You can highlight different parts of text with different colors using multiple tags ?
from tkinter import *
win = Tk()
win.geometry("700x350")
win.title("Multiple Colors Example")
frame = Frame(win)
text = Text(frame, font=("Arial", 12))
# Insert multiple lines of text
text.insert(INSERT, "Python is a powerful language.\n")
text.insert(INSERT, "Tkinter makes GUI development easy.\n")
text.insert(INSERT, "Text highlighting improves readability.")
# Highlight "Python" with blue background
text.tag_add("python", "1.0", "1.6")
text.tag_configure("python", background="lightblue", foreground="darkblue")
# Highlight "Tkinter" with green background
text.tag_add("tkinter", "2.0", "2.7")
text.tag_configure("tkinter", background="lightgreen", foreground="darkgreen")
# Highlight "highlighting" with yellow background
text.tag_add("highlight", "3.5", "3.17")
text.tag_configure("highlight", background="yellow", foreground="red")
text.pack()
frame.pack()
win.mainloop()
Dynamic Text Selection
You can also highlight text based on user selection or search results ?
from tkinter import *
def highlight_word():
# Clear existing tags
text.tag_remove("highlight", "1.0", END)
# Get the word to highlight
word = entry.get()
# Find and highlight all occurrences
start_pos = "1.0"
while True:
start_pos = text.search(word, start_pos, END)
if not start_pos:
break
end_pos = f"{start_pos}+{len(word)}c"
text.tag_add("highlight", start_pos, end_pos)
start_pos = end_pos
# Configure the highlight color
text.tag_configure("highlight", background="orange", foreground="black")
win = Tk()
win.geometry("700x400")
win.title("Dynamic Highlighting")
# Entry widget for search term
Label(win, text="Enter word to highlight:").pack(pady=5)
entry = Entry(win, width=30)
entry.pack(pady=5)
Button(win, text="Highlight", command=highlight_word).pack(pady=5)
# Text widget with sample content
text = Text(win, font=("Arial", 11))
sample_text = """Python is a versatile programming language.
Python is used for web development, data science, and automation.
Learning Python opens many career opportunities."""
text.insert("1.0", sample_text)
text.pack(fill=BOTH, expand=True, padx=10, pady=10)
win.mainloop()
Common Tag Configuration Options
| Option | Description | Example |
|---|---|---|
background |
Background color of text | background="yellow" |
foreground |
Text color | foreground="red" |
font |
Font family and style | font=("Arial", 12, "bold") |
underline |
Underline the text | underline=True |
overstrike |
Strike through text | overstrike=True |
Output
Running the above code will display a text widget with highlighted text. The first example shows basic highlighting with black background and yellow text. The multiple color example demonstrates different highlighting styles for various words.
Conclusion
Use tag_add() to select text portions and tag_configure() to apply colors and styles. This approach is essential for creating text editors, syntax highlighters, and search functionality in Tkinter applications.
