Search code examples
pythontkintertext-stylingtext-widget

Tags Incorporated in String in Tkinter Text Widget


Tkinter Text widget allows to style text using tags.

text.tag_config('red', foreground='red')
text.insert(END, 'Red text', 'red')

Is it possible to include the tag in the string literal?

Pseudocode:

text.insert(END, 'This sentence has <red tag>red text</red tag> in it.')

Solution

  • No, it is not possible. However, it's pretty easy to write code that searches for patterns in the text widget and to replace them with tkinter tags.