Adding coloured text to selected text in Tkinter
Last updated Dec 31, 2021In a Tkinter text widget, you might want to display different colors for different words. This could be for a number of different reasons. For example, you might want to highlight some text in a text widget with a different color to draw the user's attention to these texts.
In Tkinter, the tag add() method is used when you want to add different colors to a text box. Use this method to choose a specific text in a text widget by giving it a number of different things. In addition, you can make certain text stand out by changing the color of its background. This can be done with the tag configure () method.
Let's look at the code below which adds a coloured text to a particular text in a Tkinter text widget.
from tkinter import * win = Tk() win.geometry("500x250") frame= Frame(win) text= Text(frame) text.insert(INSERT, "Hey!!, You can Start Python Programming Now") text.pack() text.tag_add("start", "1.7", "1.32") text.tag_configure("start", background= "black", foreground= "red") frame.pack() win.mainloop()
|
Output
The above code show a colored text of the chosen text in Tkinter text widget
![]() |
Article Contributed By :
|
|
|
|
1846 Views |