- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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.