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 thetag/iditem. If there is more than one match fortag/id, the last item found will ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access