namespace eval name { ... }
variable var_name [value] variable var_name1 value1 ... var_nameN [valueN]
variale var
リスト : 名前空間の使用例1 proc test_foo {} { puts "Global foo!!" } namespace eval Foo { variable foo 100 proc test_foo {} { variable foo puts "Foo::foo is $foo" } proc test_bar {} { # Foo::test_foo を呼び出す test_foo # グローバルな test_foo を呼び出す ::test_foo } } namespace eval Bar { proc test_bar {} { # グローバルな test_foo を呼び出す test_foo } } puts $Foo::foo Foo::test_bar Bar::test_bar
C>tclsh test00.tcl 100 Foo::foo is 100 Global foo!! Global foo!!
リスト : 名前空間の使用例2 proc test_foo {} { puts "Global foo!!" } namespace eval Foo { variable foo 100 proc test_foo {} { variable foo puts "Foo::foo is $foo" } proc test_bar {} { # Foo::test_foo を呼び出す test_foo # グローバルな test_foo を呼び出す ::test_foo } namespace eval Bar { variable bar 200 proc test_bar {} { # グローバルな test_foo を呼び出す test_foo } } } puts $Foo::foo puts $Foo::Bar::bar Foo::test_bar Foo::Bar::test_bar
C>tclsh test01.tcl 100 200 Foo::foo is 100 Global foo!! Global foo!!
namespace export func1 func2 ...
namespace import name::func1 name::func2 ... namespace import name::*
リスト : 名前空間の使用例3 namespace eval Foo { namespace export foo bar proc foo {} { puts "foo!!" } proc bar {} { puts "bar!!" } proc baz {} { puts "baz!!" } } namespace eval Bar { namespace import ::Foo::* namespace export baz proc baz {} { foo bar } } namespace import Bar::* baz Foo::baz
C>tclsh test02.tcl foo!! bar!! baz!!
リスト : 名前空間の使用例4 namespace eval Hello { variable var {Hello World} cnt 0 end 0 proc print {} { variable var variable cnt variable end puts $var incr cnt if {$cnt >= 10} { set end 1 } else { # after 500 Hello::print でも動作する after 500 [namespace code print] } } } Hello::print vwait Hello::end
C>tclsh test03.tcl Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World C>
% set d [dict create foo 10 bar 20 baz 30] foo 10 bar 20 baz 30 % dic exists $d foo 1 % dic exists $d oops 0 % dic get $d bar 20 % dic get $d oops key "oops" not known in dictionary % dict set d baz 300 foo 10 bar 20 baz 300 % dict get $d baz 300 % dict set d oops 40 foo 10 bar 20 baz 300 oops 40 % dict unset d foo bar 20 baz 300 oops 40 % dict exists $d foo 0 % dict size $d 3
% set d [dict create a 0] a 0 % dict append d a 1 2 3 4 5 a 012345 % dict set d b {1 2 3 4} a 012345 b {1 2 3 4} % dict append d b 5 6 7 8 a 012345 b {1 2 3 45678} % dict lappend d b 9 10 11 12 a 012345 b {1 2 3 45678 9 10 11 12} % dict lappend d a 9 10 11 12 a {012345 9 10 11 12} b {1 2 3 45678 9 10 11 12} % set d [dict create a 1 b 2 c 3] a 1 b 2 c 3 % dict incr d a a 2 b 2 c 3 % dict incr d b a 2 b 3 c 3 % dict incr d c a 2 b 3 c 4 % dict keys $d a b c % dict values $d 2 3 4 % dict for {k v} $d { puts $k puts $v } a 2 b 3 c 4
リスト : Ttk Widget の簡単な使用例 # テーマの取得と設定 if {[llength $argv] == 0} { set theme_name [ttk::style theme use] } else { set theme_name [lindex $argv 0] ttk::style theme use $theme_name } # Ttk ウィジェットのフォントを指定 ttk::style configure . -font {{} 12} # ラジオボタン用変数 set v 0 # チェックボタン用変数 foreach x {0 1 2 3} { set opts($x) 1 } # テーマ表示用ラベル ttk::label .la -text $theme_name pack .la # ボタン類の配置 ttk::labelframe .f0 -text "Buttons" ttk::labelframe .f1 -text "CheckButtons" ttk::labelframe .f2 -text "RadioButtons" foreach x {0 1 2 3} { ttk::button .b$x -text "button $x" pack .b$x -in .f0 ttk::radiobutton .r$x -text "radiobutton $x" -value $x -variable v pack .r$x -in .f1 ttk::checkbutton .c$x -text "checkbutton $x" -variable opts($x) pack .c$x -in .f2 } pack .f0 .f1 .f2 -padx 5 -pady 5 -side left
リスト : Ttk Widget の簡単な使用例 (Windows の場合、文字コードは sjis にすること) ttk::style theme use winnative ttk::style configure test1.TLabel -font {{Ricty Diminished} 12} ttk::style configure test2.TLabel -font {{Ricty Diminished} 14} -foreground blue ttk::style configure test3.TLabel -font {{Ricty Diminished} 16 italic} -foreground green set str "Hello, world, こんにちは世界" ttk::label .l0 -text $str -style test1.TLabel ttk::label .l1 -text $str -style test2.TLabel ttk::label .l2 -text $str -style test3.TLabel pack .l0 .l1 .l2
ttk::combobox pathname -option value ...
-height | リストボックスの行数 |
-width | エントリーの文字数 |
-state | normal : エントリーとリストボックスの両方から入力できる readonly : リストボックスのみ有効 disabled : どちらも無効 |
values | リストボックスに表示するデータを格納したリスト |
リスト : コンボボックスの使用例 # n 以上 m 以下の整数列を生成する proc iota {n m} { set xs {} while {$n <= $m} { set xs [linsert $xs end $n] incr n } return $xs } # フォントの指定 ttk::style configure . -font {{} 14} # ラベル用の変数 set label_buff "" set year_buff 2019 set month_buff 1 set day_buff 1 ttk::label .la -textvariable label_buff pack .la ttk::label .ly -text " Year: " pack .ly -side left ttk::combobox .year_cb -textvariable year_buff -width 8 -value [iota 1970 2030] pack .year_cb -side left ttk::label .lm -text " Month: " pack .lm -side left ttk::combobox .month_cb -textvariable month_buff -width 8 -height 12 -value [iota 1 12] pack .month_cb -side left ttk::label .ld -text " Day: " pack .ld -side left ttk::combobox .day_cb -textvariable day_buff -width 8 -value [iota 1 31] pack .day_cb -side left # 日付の確認 proc check_date {y m d} { set date1 [format "%02d/%02d/%d" $m $d $y] if {[clock format [clock scan $date1] -format "%D"] == $date1} { return 1 } else { return 0 } } # 入力データの表示 proc get_item {} { global label_buff set y [.year_cb get] set m [.month_cb get] set d [.day_cb get] if {[check_date $y $m $d]} { set label_buff [format "%02d/%02d/%d" $m $d $y] } else { set label_buff "Input Error" } } bind .year_cb <<ComboboxSelected>> { get_item } bind .year_cb <Return> { get_item } bind .month_cb <<ComboboxSelected>> { get_item } bind .month_cb <Return> { get_item } bind .day_cb <<ComboboxSelected>> { get_item } bind .day_cb <Return> { get_item } # 初期値を表示する get_item
初期状態
日付の入力
エラー
pathname add widget -option value ...
リスト : ノートブックの使用例 (1) ttk::style configure TButton -font {{} 16} ttk::style configure TRadiobutton -font {{} 16} ttk::style configure TCheckbutton -font {{} 16} # ラジオボタンとチェックボタンの変数 set v 0 foreach x {0 1 2 3} { set opts($x) 1 } # ノートブック ttk::notebook .nb pack .nb # ボタン類の配置 ttk::frame .f0 ttk::frame .f1 ttk::frame .f2 foreach x {0 1 2 3} { ttk::button .f0.b$x -text "button $x" pack .f0.b$x ttk::radiobutton .f1.r$x -text "radiobutton $x" -value $x -variable v pack .f1.r$x ttk::checkbutton .f2.c$x -text "checkbutton $x" -variable opts($x) pack .f2.c$x } .nb add .f0 -text Button -padding 20 .nb add .f1 -text Radio -padding 20 .nb add .f2 -text Check -padding 20
初期状態 (Button が選択されている)
Radio を選択
Check を選択
リスト : ノートブックの使用例 (2) # テキストウィジェットのフォントを設定する option add *Text.font {{} 14} # 生成したテキストウィジェットの個数 set text_count 0 # ノートブック ttk::notebook .nb # テキストウィジェットを追加 proc new_text {} { global text_count incr text_count text .tw$text_count -width 40 -height 10 .nb add .tw$text_count -text "Memo $text_count" .nb select .tw$text_count } # テキストウィジェットを削除 proc del_text {} { .nb forget current } # ボタン ttk::frame .fr ttk::button .fr.bnew -text New -command new_text pack .fr.bnew -side left ttk::button .fr.bdel -text Del -command del_text pack .fr.bdel -side left # 配置 pack .fr -anchor w pack .nb
初期状態
Memo1 を生成
Memo2 を生成
Memo3 を生成して Memo2 を削除
リスト : プログレスバーの使用例 set var 0 set pb1_id "" proc exec_pb1 {} { global var pb1_id incr var if {$var < 100} { set pb1_id [after 40 exec_pb1] } } proc start_pb1 {} { global var set var 0 exec_pb1 } proc stop_pb1 {} { global pb1_id if {$pb1_id != ""} { after cancel $pb1_id set pb1_id "" } } ttk::progressbar .pb1 -length 200 -variable var ttk::progressbar .pb2 -length 200 -mode indeterminate pack .pb1 ttk::frame .f1 ttk::button .f1.b1 -text Start -command start_pb1 ttk::button .f1.b2 -text Stop -command stop_pb1 pack .f1.b1 .f1.b2 -side left pack .f1 pack .pb2 ttk::frame .f2 ttk::button .f2.b3 -text Start -command { .pb2 start 10 } ttk::button .f2.b4 -text Stop -command { .pb2 stop } pack .f2.b3 .f2.b4 -side left pack .f2
初期状態
途中経過
pb1 が終了し、pb2 を停止した状態
リスト : ツリービューの簡単な使用例 (1) ttk::style configure . -font {{} 12} ttk::treeview .tview -columns {Name Height} .tview column #0 -width 50 .tview column Name -anchor w -width 100 .tview column Height -anchor e -width 50 .tview heading #0 -text Class .tview heading Name -text Name .tview heading Height -text Height set id [.tview insert "" end -text A] .tview insert $id end -values {Ada 148.7} .tview insert $id end -values {Hanna 154.2} .tview insert $id end -values {Miranda 148.2} set id [.tview insert "" end -text B] .tview insert $id end -values {Alice 149.5} .tview insert $id end -values {Janet 147.8} .tview insert $id end -values {Sara 153.1} set id [.tview insert "" end -text C] .tview insert $id end -values {Carey 133.7} .tview insert $id end -values {Linda 154.6} .tview insert $id end -values {Tracy 138.2} set id [.tview insert "" end -text D] .tview insert $id end -values {Ellen 157.9} .tview insert $id end -values {Maria 159.1} .tview insert $id end -values {Violet 138.7} ttk::scrollbar .sb -orient v -command { .tview yview } .tview configure -yscrollcommand { .sb set } pack .tview -side left pack .sb -side left -fill y
初期状態
クラス A を表示
クラス A と C を表示
全クラスを表示
pathname set item [column] [value]
リスト : 身長の更新機能を追加 ttk::style configure . -font {{} 12} # グローバル変数 set buff1 "" set buff2 "" set select_item "" # 名前表示用ラベルと慎重入力用エントリー ttk::frame .fr ttk::label .fr.la -textvariable buff1 ttk::entry .fr.e -width 8 -textvariable buff2 pack .fr.la .fr.e -side left pack .fr ttk::treeview .tview -columns {Name Height} .tview column #0 -width 50 .tview column Name -anchor w -width 100 .tview column Height -anchor e -width 50 .tview heading #0 -text Class .tview heading Name -text Name .tview heading Height -text Height set id [.tview insert "" end -text A] .tview insert $id end -values {Ada 148.7} .tview insert $id end -values {Hanna 154.2} .tview insert $id end -values {Miranda 148.2} set id [.tview insert "" end -text B] .tview insert $id end -values {Alice 149.5} .tview insert $id end -values {Janet 147.8} .tview insert $id end -values {Sara 153.1} set id [.tview insert "" end -text C] .tview insert $id end -values {Carey 133.7} .tview insert $id end -values {Linda 154.6} .tview insert $id end -values {Tracy 138.2} set id [.tview insert "" end -text D] .tview insert $id end -values {Ellen 157.9} .tview insert $id end -values {Maria 159.1} .tview insert $id end -values {Violet 138.7} ttk::scrollbar .sb -orient v -command { .tview yview } .tview configure -yscrollcommand { .sb set } pack .tview -side left pack .sb -side left -fill y # 生徒の選択 proc select_student {} { global select_item buff1 buff2 set xs [.tview selection] set x [lindex $xs 0] set d [.tview set $x] if {$d != {}} { set buff1 [dict get $d Name] set buff2 [dict get $d Height] set select_item $x } else { set buff1 "" set buff2 "" set select_item "" } } # 身長の更新 proc update_height {} { global select_item buff2 if {$select_item != ""} { .tview set $select_item Height $buff2 } } # バインディング bind .fr.e <Return> { update_height } bind .tview <<TreeviewSelect>> { select_student }
Alice を選択
エントリーで身長を入力
リターンキーで更新