以下の例は、テキストWidget内のJava,Tcl,Cのキーワードに対してタグを付けて、そのキーワードの色を変更するバインドをしています。
pack [text .t -width 30 -height 7 -bg white] .t insert end { 主なコンピュータ・プラットフォームはすべて、CまたはJavaのような\ システム・プログラミング言語、およびTclのようなスクリプト言語の両方を提供します。 } set pos 1.0 while {1} { catch {.t search -count length -regexp "Java|Tcl|C" $pos end} pos if {$pos == ""} { break } .t tag add foo $pos "$pos + $length char" set pos [.t index "$pos + $length char"] } .t tag configure foo -background blue -foreground white .t tag bind foo <Enter> { %W tag configure foo -background red } .t tag bind foo <Leave> { %W tag configure foo -background blue } |
次の例は、キャンバスWidget内の図形に対してタグを付けて、その図形の輪郭の色を変更するバインドをしています。
pack [canvas .c -height 70 -width 70 -bg white] .c create oval 10 10 40 40 -fill red -width 4 -tag foo .c create oval 30 10 60 40 -fill green -width 4 -tag foo .c create oval 20 30 50 60 -fill blue -width 4 -tag foo .c bind foo <Enter> { %W itemconfigure foo -outline gold } .c bind foo <Leave> { %W itemconfigure foo -outline black } |
タグ名を付けるときの注意として、
キャンバスWidgetでは、allという名前のタグ名はすべてのタグを指すために予約されているという点です。
ただし、テキストWidgetでは予約されていません。仕様に一貫性がないように思います。(^^;)