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 highlight the current line of a Text widget in Tkinter?
We can use the Tkinter Text widget to accept multiline user input. We can insert text, display information, and get the output from the text widget.
To highlight the current line or selected text in a Text widget, we can use the tag_add() method that adds a tag to specific text ranges, combined with tag_configure() to define the highlighting style.
Basic Text Highlighting
Here's how to highlight specific text in a Text widget ?
import tkinter as tk
# Create an instance of tkinter window
win = tk.Tk()
win.geometry("700x350")
# Add a text widget
text = tk.Text(win, width=80, height=15, font=('Calibri', 12))
# Insert default text
text.insert(tk.INSERT, "Tkinter is a Python Library to create GUI-based applications.\n")
text.insert(tk.END, "Learning Tkinter is Awesome!!")
# Highlight text by adding tags
text.tag_add("highlight", "1.0", "1.7") # Highlight first 7 characters of line 1
text.tag_configure("highlight", background="OliveDrab1", foreground="black")
text.pack()
win.mainloop()
Highlighting the Current Line
To highlight the entire line where the cursor is positioned ?
import tkinter as tk
def highlight_current_line():
# Remove previous highlighting
text.tag_remove("current_line", "1.0", tk.END)
# Get current cursor position
cursor_pos = text.index(tk.INSERT)
line_start = cursor_pos.split('.')[0] + '.0'
line_end = cursor_pos.split('.')[0] + '.end'
# Add tag to current line
text.tag_add("current_line", line_start, line_end)
text.tag_configure("current_line", background="lightblue")
win = tk.Tk()
win.geometry("600x400")
text = tk.Text(win, width=60, height=20, font=('Arial', 11))
text.insert("1.0", "Line 1: Click on any line\nLine 2: to see it highlighted\nLine 3: This demonstrates current line highlighting\nLine 4: Move cursor around!")
# Bind events to highlight current line
text.bind("<KeyRelease>", lambda e: highlight_current_line())
text.bind("<ButtonRelease-1>", lambda e: highlight_current_line())
text.pack(pady=10)
win.mainloop()
Multiple Highlight Styles
You can create different highlighting styles for various purposes ?
import tkinter as tk
win = tk.Tk()
win.geometry("600x300")
text = tk.Text(win, width=60, height=15, font=('Courier', 10))
# Insert sample text
sample_text = """Important: This is a critical message
Warning: Please read this carefully
Error: Something went wrong
Info: Just for your information"""
text.insert("1.0", sample_text)
# Configure different highlight styles
text.tag_configure("important", background="red", foreground="white")
text.tag_configure("warning", background="orange", foreground="black")
text.tag_configure("error", background="pink", foreground="red")
text.tag_configure("info", background="lightgreen", foreground="darkgreen")
# Apply highlights to different lines
text.tag_add("important", "1.0", "1.end")
text.tag_add("warning", "2.0", "2.end")
text.tag_add("error", "3.0", "3.end")
text.tag_add("info", "4.0", "4.end")
text.pack(pady=10)
win.mainloop()
Key Methods
| Method | Purpose | Example |
|---|---|---|
tag_add() |
Add tag to text range | text.tag_add("tag", "1.0", "1.5") |
tag_configure() |
Set tag appearance | text.tag_configure("tag", bg="yellow") |
tag_remove() |
Remove tag from range | text.tag_remove("tag", "1.0", "end") |
Conclusion
Use tag_add() and tag_configure() to highlight text in Tkinter Text widgets. Bind keyboard and mouse events to dynamically highlight the current line as users navigate through the text.
