8.5. Text Tags

Text tags give you another way to address portions of text in the text widget. A tag has three purposes, and the same tag can serve all three or only one:

  • Assigning formatting information to a portion(s) of text

  • Associating a binding with text in the widget

  • Managing selected text

Tags are also used to change how the text appears on the screen: font, size, coloring, and spacing are among a few of the text properties affected by tags. You change text properties by creating your own tags (with their own names), and using option/value pairs to assign formatting information. In addition to changing the formatting, you can use a tag to apply a specific binding (such as perform a task when the user clicks on that text). A special tag "sel" manages the selected text. Anytime the user selects some text, the location of that text is marked with the tag "sel".

Any of the text within the text widget can have one or more tags associated with it. If you apply two tags to the same piece of text and they both alter the font, the last tag applied wins.

8.5.1. Options Used With Tags

The options you can use to configure tagged text are mostly a subset of the configuration options of the text widget itself. There are some options that can only be used through tagged text.

-background => color

Sets the color of the area behind the text.

-bgstipple => pattern

Sets the pattern used to draw the area behind the text. Can create a shaded look.

-borderwidth => amount

Sets the width of ...

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.