tags

Tags are strings that are attached to items on a complex widget (text or canvas), or to groups of bindings (see bindtags). An item can have several tags. After assigning them, you can perform actions on a tag, which will be done with all the tagged items.

In text widgets, tags are the only way to assign attributes (font, colors, underline..) to text portions.

In canvas widgets, tags are a convenient way of grouping items, and configuring whole groups of them in one command (see Flag signalling for examples). But items may also be configured individually there (or, put differently, the numerical id assigned to each item also is a unique tag). (RS)

Usage examples:


 package require Tk
 canvas .c
 # On canvases, tags are known only if at least one item has it
 foreach i {1 2 3 4 5} {
     .c create rect [expr $i*10] 0 [expr $i*10+8] 8 -tag box
 } ;# create multiple canvas objects
 .c itemconfig box -fill red ;# to change their color
 .c lower box ;# to hide them behind a background area
 pack .c

 text .t
 # On text widgets, tags persist even without instances
 # You should configure them early, to avoid lengthy reformatting
 .t tag config bold   -font {helvetica 12 bold}
 .t tag config hilite -background orange
 .t insert end Hello
 .t insert end world bold ;# tags come as 4(6,8..)th element
 .t insert end "out there" {bold hilite}
 .t tag config bold -font {times 11 bold}  ;# change and reformat all
 pack .t

Note: Use the full names for foreground and background. In the tag config context fg means fgstipple and bg means bgstipple.

[tutorials]

[examples in books, including Effective, JO's, ...]


"Tagging passages of text or canvas objects makes it possible to apply formatting to or recognize events from the whole collection... with a concise syntactic specification... not heaps of application specific code." Jon Fernquest [L1 ]