

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
# 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") # 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()
Output
Running the above code will display a text widget with some highlighted text.
- Related Questions & Answers
- How to word-wrap text in Tkinter Text?
- How to highlight text in a tkinter Text widget?
- Underline Text in Tkinter Label widget
- How do I center the text in a Tkinter Text widget?
- How to insert text at the beginning of the text box in Tkinter?
- How to get the Tkinter Label text?
- How to change text cursor color in Tkinter?
- How To Dynamically Resize Button Text in Tkinter?
- How to set font for Text in Tkinter?
- How to take input in a text widget and display the text in tkinter?
- Adding a text plus and text written from a parameter type C in ABAP
- Adding Hyphens to Text with the CSS hyphens Property
- Select all text in a Text widget using Python 3 with tkinter
- How to get the current length of the Text in a Tkinter Text widget?
- Add advanced features to a tkinter Text widget