Tags

Each item can also have a tag (or more than one tag) associated with it. We have seen tags used before in the Text widget, where sections of text could be assigned a tag. A tag can be assigned when the item is created, or you can use the addtag method to assign a tag after the item has been created.

There are two special tags that are assigned and maintained automatically: "current" and "all". The "all" tag refers to all the items in the Canvas. The "current" tag refers to the topmost item that the mouse cursor is over. If the mouse cursor is outside the Canvas widget or not over an item, the "current" tag does not exist.

You can use tags to make changes to many different items at once. For instance, if you want all circles to have the same color, but you want to be able to change it from time to time, give all circles a "circle" tag when you create them, using the itemconfigure method to change the configuration options of the items with the "circle" tag.

Tags can also be logical expressions by using the operators: &&, ||, ^, !, and parenthetical subexpressions. For example:

$c->find('withtag',  '(a&&!b)||(!a&&b)');

or equivalently:

$c->find('withtag', 'a^b');

will find only those items with either a or b tags, but not both.

The following are some sample syntax lines for creating tags:

$canvas->addtag("newtag", "above", tag/id );

The "newtag" tag is added to the item that is above the tag/id item. If there is more than one match for tag/id, the last item found will ...

Get Mastering Perl/Tk 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.