
|
たぶラーリストボックス(寿司ネタ炸裂編) たぶラーリストボックスとは、タブのついたラーリストボックスです(コロス) あーっ、うそうそうそうそうそうそ、 Tabular List 訳してたぶラーリスト、ですね (訳してないってば) 名前の通りTkのリストボックスと同様、 複数の項目から1個ないし何個かの項目をマウスでつつくその他の方法によって選ぶためのウィジェットですが、 Tkのリストボックスよりも表現力が豊かです。 華やかで機能的な画面を作りたいクリエイティヴャな皆さんにぴったんこ(死語)
package require Tix
wm title . "Sushi Rotation I"
set fa [frame .fa -rel groove -bd 2]
tixTList $fa.ta -width 40 -height 10 -bg #f0c0c0 -fg #600040 \
-yscrollcommand "$fa.scrv set" \
-selectforeground white -selectbackground #600030 \
-orient vertical \
-browsecmd "BrowsedCmd $fa.ta" -command "ActivatedCmd $fa.ta"
# どれかの項目が選択状態になったときに呼ばれるコマンド。
# 選択された項目のインデックス(先頭が零、下のindex)がつけられます。
proc BrowsedCmd {w index} {
set itemtext [$w entrycget $index -text]
.ea delete 0 end
.ea insert end "${itemtext}? 今日はうまいよ!"
}
# どれかの項目が実行(ENTERを押すかダブルクリック)されたときに呼ばれるコマンド。
# 選択された項目のインデックス(先頭が零、下のindex)がつけられます。
proc ActivatedCmd {w index} {
set itemtext [$w entrycget $index -text]
.ea delete 0 end
.ea insert end "カウンター4番さん ${itemtext}いっちょう!"
}
scrollbar $fa.scrv -orient vertical -command "$fa.ta yview"
pack $fa.ta $fa.scrv -side left -fill y
set netas {
穴子 いか いくら えび さば いわし かにサラダ ねぎとろ はまち まぐろ トロ
}
foreach e $netas {
$fa.ta insert end -itemtype text -text $e
}
entry .ea -width 40
button .b -text "終了" -command exit
pack $fa .ea .b -side top -anc e
# end.
たぶラーリストボックスはtixTList コマンドで作ります。 -widthと-heightは文字数で指定しますが、 正確にその数の項目が並べられるわけではありません (普通表示できる項目の数は、指定した文字数の7割くらい) で、たぶラーリストのリストボックスと違うところは、 あまった項目は折り返して並べられることです。 -orientをverticalにすると、上から下に並べられるだけ並べ、 次にその右に移ってまた上から下…という流れになります。 horizontalにすると左から右に並べ、次にその下に移ってまた左から右… となります。
はてさて、二つ目の喜ばしい機能として、 たぶラーリストでは各項目ごとに色やフォントを変えることができます。 その際に使うのが ディスプレイスタイル (tixDisplayStyle)と呼ばれるものです。 下のサンプルをご覧ください。
package require Tix
wm title . "Sushi Rotation II"
tixDisplayStyle text -stylename style1 -fg #008000 -bg #80f0c0 \
-selectforeground red -selectbackground #80f0c0
tixDisplayStyle text -stylename style2 -fg #000080 -bg #80c0f0 \
-selectforeground red -selectbackground #80c0f0
set fa [frame .fa -rel groove -bd 2]
tixTList $fa.ta -width 40 -height 10 -bg #f0c0c0 -ysc "$fa.scrv set" \
-selectforeground white -selectbackground #600030 -orient vertical
scrollbar $fa.scrv -orient vertical -command "$fa.ta yview"
pack $fa.ta $fa.scrv -side left -fill y
set netas {
穴子 いか いくら えび さば いわし かにサラダ ねぎとろ はまち まぐろ トロ
}
set i 0
foreach e $netas {
$fa.ta insert end -itemtype text -text $e -style style[expr $i%2+1]
incr i
}
entry .ea -width 40
button .b -text "終了" -command exit
pack $fa .ea .b -side top -anc e
# end.
項目の前景色や背景色を変えるには、 まずそれらの指定をワンセットにしたtixDisplayStyle を作っておき、実際に項目を追加するときにその configureオプション -style でそのスタイルを指定します。 複数の項目について同じスタイルを使うことも当然可能です。 tixDisplayStyle text -stylename style1 -fg #008000 -bg #80f0c0 \ -selectforeground red -selectbackground #80f0c0 $fa.ta insert end -itemtype text -text $e -style style1最後は項目にアイコンを貼りつけたサンプルです。
package require Tix
wm title . "Sushi Rotation III"
set a [tixDisplayStyle text -fg #600030]
set fa [frame .fa -rel groove -bd 2]
tixTList $fa.ta -width 30 -height 20 -yscrollcommand "$fa.scrv set" \
-command "selected $fa.ta"
scrollbar $fa.scrv -orient vertical -command "$fa.ta yview"
pack $fa.ta $fa.scrv -side left -fill y
set netas {
穴子 いか いくら えび さば いわし かにサラダ ねぎとろ はまち まぐろ トロ
}
foreach e $netas {
set Images($e) [\
image create photo -file /usr/icons/freeso/susipng/寿司$e.gif]
$fa.ta insert end -itemtype imagetext \
-text [lindex $e 0] -image $Images($e)
}
button .b -text "終了" -command exit
pack $fa .b -side top -anc e
proc selected {w index} {
set itemtext [$w entrycget $index -text]
tk_messageBox -message "はいカウンター5番さん ${itemtext}いっちょう!"
}
# end.
あー、 寿司ネタの炸裂っぷりに私的にはもう満腹であります。 それじゃ、私はこれで(待て待て待て) 画像データを貼る方法は、 グリッドやハイアラキカルリストで出てきたのと同様、 項目のconfigureオプション -itemtype で imagetext を指定し、 -image で表示したい画像データを指定します。
$fa.ta insert end -itemtype imagetext \
-text トロ -image ImageToro
なお寿司ネタアイコンはKAZUTAKAさんの アイコンミュージアム
|