Adding coloured text to selected text in Tkinter
Tkinter is a powerful and versatile GUI toolkit for Python. It offers a range of user interface widgets and tools to create graphical applications easily. One of the most fundamental features of Tkinter is the text widget. It allows the user to create and edit text content in various formats. In this tutorial, we will show how to add coloured text to selected text in Tkinter using Python.
Prerequisites
To follow this tutorial, you should have Python 3.x, Tkinter installed on your system.
Creating a Text Widget in Tkinter
The first step is to create a text widget in Tkinter. We use the Text
widget to display and edit the text. Here is a sample code snippet to create a text widget:
import tkinter as tk
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.pack()
root.mainloop()
Here, we import the tkinter
module and create a Text
widget inside a Tkinter window. We pack the widget in the window and run the event loop mainloop
to display the window.
Adding Text to the Widget
Now, we can add some text content to the widget. We can use the insert
method to insert text at the specified index. Here is an example:
import tkinter as tk
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.pack()
text_widget.insert('1.0', 'Hello, world!')
root.mainloop()
Here, we add the text “Hello, world!” at index ‘1.0’, which represents the beginning of the text widget.
Adding Coloured Text
To add coloured text, we need to use the tag_config
method. We can create a tag that specifies a foreground colour and apply it to the selected text. Here is an example:
import tkinter as tk
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.pack()
text_widget.insert('1.0', 'Hello, world!')
tag_name = 'color_tag'
text_widget.tag_config(tag_name, foreground='red')
text_widget.tag_add(tag_name, '1.0', '1.5')
root.mainloop()
Here, we create a new tag named color_tag
with a foreground colour of red
. We add the tag to the selected text range from index ‘1.0’ to index ‘1.5’, which corresponds to the first five characters of the text.
The tag_config
method accepts various options to configure the tag, such as font and background colour.
Highlighting Selections
We can use the tag_configure
method to highlight the current selection. We create a new tag for the highlight and apply it to the selected text. Here is an example:
import tkinter as tk
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.pack()
text_widget.insert('1.0', 'Hello, world!')
text_widget.tag_config('highlight', background='yellow')
def on_select(event):
text_widget.tag_add('highlight', 'sel.first', 'sel.last')
text_widget.bind('<ButtonRelease-1>', on_select)
root.mainloop()
Here, we create a new tag named highlight
with a yellow background colour. We define a new function on_select
to handle the selection event. When the user selects some text, we call tag_add
to apply the highlight
tag to the selected range.
Finally, we bind the ButtonRelease-1
event to the on_select
function, which triggers the function when the user releases the left mouse button.
Conclusion
In this tutorial, we have learned how to add coloured text to selected text in Tkinter using Python. We created a text widget, added text content, and created tags to specify text properties such as foreground and background colour. We also applied tags to selected text ranges and highlighted selections using tags. With these techniques, you can create sophisticated text editing applications in Python and Tkinter. Happy coding!