- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the color of certain words in a Tkinter text widget?
Tkinter text widgets are used to create and display multiline text Input. It provides several functions and methods that are generally used to configure a text widget.
Let us suppose we want to change the color of certain words in a text widget, then we can use the tag_add(tag name, range) method which selects the word we want to format. Once the word has been selected, we can change its color, background color, and other properties using the tag_config(properties) method.
In this example, we will configure the color of a selected word in the text widget.
#Import required libraries from tkinter import * #Create an instance of tkinter window win =Tk() #Define the geometry of the window win.geometry("600x250") #Create a text widget text= Text(win) text.insert(INSERT, "Hello World!\n") text.insert(END, "This is a New Line") text.pack(fill=BOTH) #Configure the text widget with certain color text.tag_config("start", foreground="red") text.tag_add("start", "1.6", "1.12") win.mainloop()
Running the above code will display a window with a text containing the string “Hello World” where “World” contains some specific color.
- Dynamically change the widget background color in Tkinter
- How to change text cursor color in Tkinter?
- How to change the menu background color of Tkinter's OptionMenu widget?
- How to highlight text in a tkinter Text widget?
- Change the focus from one Text widget to another in Tkinter
- How to clear the contents of a Tkinter Text widget?
- How to get the current length of the Text in a Tkinter Text widget?
- Get the text of a button widget in Tkinter
- How to set the border color of certain Tkinter widgets?
- Python Tkinter – How do I change the text size in a label widget?
- How do I center the text in a Tkinter Text widget?
- How to change the color of a Tkinter label programmatically?
- How to fully change the color of a Tkinter Listbox?
- How to highlight the current line of a Text widget in Tkinter?
- How to create hyperlink in a Tkinter Text widget?