Tags

You can associate a distinct set of format properties to a portion of the text using tags. A tag is defined with the tagConfigure method, and text is associated with a tag via an option to the insert or tagAdd method. For example:

$text->Text->pack;
$text->tagConfigure('bold', -font =>
     '-*-Courier-Medium-B-Normal-*-120-*-*-*-*-*-*');
$text->insert('end', "Normal text\n");
$text->insert('end', "Bold text\n", 'bold');

There are several methods defined for manipulating text tags:

tagAdd

Adds a tag to the text within the specified index range. For example, to assign the bold tag defined above to the current selection:

$text->tagAdd('bold','sel.first','sel.last');

You can supply multiple ranges as arguments to tagAdd.

tagBind

Executes a callback when a specified event happens on the tagged text. For example:

$text->tagBind('goto_end', "<Button-1>", sub {shift->see('end');} );
tagConfigure

Creates or changes settings for a text tag. For example:

$text->tagConfigure('link', -foreground => 'red');

Options for configuring text tags are:

-background => color

The color to use behind the text.

-bgstipple => bitmap

A pattern to draw behind the text.

-borderwidth => amount

The width of the edge drawn around the text.

-fgstipple => bitmap

A pattern used to draw the text.

-font => fontname

The font used for the text.

-foreground => color

The color of the text.

-justify => position

The justification of the text (any of left, right, and center). The default is left.

-lmargin1 => amount

The indentation for the first line ...

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.