目次

数字リスト作成アプリ

 UNIXのOSが動作しているときに、単純な数字リストを
 作成する必要に迫られて、Tcl/TkでGUIを持ったアプリ
 ケーションを作成しました。



 ラベルの右にボタンを配置し、ボタンをクリックすると
 左に示すラベルの数字を、エントリボックスの文字列
 として追加する、簡単なアプリケーションソフト。

 ラベル、ボタンを配置し、タイプする量を減らせる
 ように、まとめていくのが目的。

 数字の右にあるボタンをクリックすると、順番にボックスに
 数字がコンマをつけて 追加されていきます。



 左側にある、数字を表示するラベルを定義。

label .lblZenpo    -text { 1} -width 4
label .lblHidarie  -text { 2} -width 4
label .lblMigie    -text { 3} -width 4
label .lblKonosaki -text { 4} -width 4
label .lblKouji    -text { 5} -width 4
label .lblCyuui    -text { 6} -width 4
label .lblYahidari -text { 7} -width 4
label .lblYamigi   -text { 8} -width 4
label .lblZhidari  -text { 9} -width 4
label .lblZmigi    -text {10} -width 4

 ボタンを定義。

button .btnZenpo    -bg gray -text {前方注意}   -command "px 1" -width 10
button .btnHidarie  -bg gray -text {左へ}       -command "px 2" -width 10
button .btnMigie    -bg gray -text {右へ}       -command "px 3" -width 10
button .btnKonosaki -bg gray -text {この先}     -command "px 4" -width 10
button .btnKouji    -bg gray -text {工事中}     -command "px 5" -width 10
button .btnCyuui    -bg gray -text {注意マーク} -command "px 6" -width 10
button .btnYahidari -bg gray -text {矢印 左}   -command "px 7" -width 10
button .btnYamigi   -bg gray -text {矢印 右}   -command "px 8" -width 10
button .btnZhidari  -bg gray -text {ゼブラ 左} -command "px 9" -width 10
button .btnZmigi    -bg gray -text {ゼブラ 右} -command "px 10" -width 10

 ボタンをクリックすると、数字をエントリボックスに
 使いすればよいので、プロシージャは、次の定義でよい
 はず。

proc px { s } {
  global tPlist
  # get current code
  set tPbox $tPlist
  # append
  if { $tPbox == "" } {
    set tPbox $s
  } else {
    set tPbox "$tPbox,$s"
  }
  # store
  set tPlist $tPbox
}

 globalを指定すると、エントリボックスで扱う
 変数を利用することになります。

 エントリボックスで扱う変数は、次のように
 定義し、コマンドでリンク。

set tPlist ""
entry .edtPlist -width 32 -bg green -textvariable tPlist

 エントリボックスの文字列を、空にすることも必要なので
 ボタンとプロシージャを定義。

button .btnClear -bg gray -text {CLEAR} -width 10 -command {
  global tPlist
  # set empty box
  set tPlist ""
}

 起動したなら、終了しなければならないので
 EXITボタンを定義。

button .btnEXIT  -bg gray -text {EXIT}  -command "exit" -width 10

 テキストファイルに、エントリボックスの内容を
 格納するには、新しいファイルを作成し、そこに
 文字列を保存すると考えれば、よいはず。

button .btnStore -bg gray -text {STORE} -width 10 -command {
  global tFileName tPlist
  if { tPlist != "" } {
    # open
    set fd_out [open $tFileName "w"]
    # store
    puts $fd_out "default = 76"
    puts $fd_out "entry_blank = 76 x 20"
    puts $fd_out "post_blank = 76 x 30" 
    puts $fd_out "loop_blank = 76 x 50"
    puts $fd_out "plist = $tPlist"
    # close
    close $fd_out
  }
}

 ここまでで、各オブジェクトで実現するメソッド
 を確定したため、各オブジェクトの配置を指定。

 ラベルは列0、ボタンは列1に配置するのなら
 gridを使い、次のように宣言すればOK。

grid .lblZenpo    -column 0 -row  1
grid .lblHidarie  -column 0 -row  2
grid .lblMigie    -column 0 -row  3
grid .lblKonosaki -column 0 -row  4
grid .lblKouji    -column 0 -row  5
grid .lblCyuui    -column 0 -row  6
grid .lblYahidari -column 0 -row  7
grid .lblYamigi   -column 0 -row  8
grid .lblZhidari  -column 0 -row  9
grid .lblZmigi    -column 0 -row 10

grid .btnZenpo    -column 1 -row  1
grid .btnHidarie  -column 1 -row  2
grid .btnMigie    -column 1 -row  3
grid .btnKonosaki -column 1 -row  4
grid .btnKouji    -column 1 -row  5
grid .btnCyuui    -column 1 -row  6
grid .btnYahidari -column 1 -row  7
grid .btnYamigi   -column 1 -row  8
grid .btnZhidari  -column 1 -row  9
grid .btnZmigi    -column 1 -row 10

 エントリボックスは列2、エントリボックスに
 関係するオブジェクトを列3に配置するのなら
 次のように指定。

grid .lblPlist -column 2 -row 2
grid .edtPlist -column 2 -row 3

grid .btnClear -column 3 -row 1
grid .btnStore -column 3 -row 2
grid .btnEXIT  -column 3 -row 11

 まとめると、次のようになります。

#!/usr/bin/wish

. configure -width 250 -height 250

wm title . "Generate water maker pattern"

# initialize file name
set tFileName   "param.txt"
set tPlist ""

# define labels
label .lblZenpo    -text { 1} -width 4
label .lblHidarie  -text { 2} -width 4
label .lblMigie    -text { 3} -width 4
label .lblKonosaki -text { 4} -width 4
label .lblKouji    -text { 5} -width 4
label .lblCyuui    -text { 6} -width 4
label .lblYahidari -text { 7} -width 4
label .lblYamigi   -text { 8} -width 4
label .lblZhidari  -text { 9} -width 4
label .lblZmigi    -text {10} -width 4
label .lblPlist    -text {Pattern code}

# define 
entry .edtPlist -width 32 -bg green -textvariable tPlist

# sub procedure
proc px { s } {
  global tPlist
  # get current code
  set tPbox $tPlist
  # append
  if { $tPbox == "" } {
    set tPbox $s
  } else {
    set tPbox "$tPbox,$s"
  }
  # store
  set tPlist $tPbox
}

# define buttons
button .btnClear -bg gray -text {CLEAR} -width 10 -command {
  global tPlist
  # set empty box
  set tPlist ""
}

button .btnStore -bg gray -text {STORE} -width 10 -command {
  global tFileName tPlist
  # open
  set fd_out [open $tFileName "w"]
  # store
  puts $fd_out "default = 76"
  puts $fd_out "entry_blank = 76 x 20"
  puts $fd_out "post_blank = 76 x 30" 
  puts $fd_out "loop_blank = 76 x 50"
  puts $fd_out "plist = $tPlist"
  # close
  close $fd_out
}

button .btnEXIT  -bg gray -text {EXIT}  -command "exit" -width 10

button .btnZenpo    -bg gray -text {前方注意}   -command "px 1" -width 10
button .btnHidarie  -bg gray -text {左へ}       -command "px 2" -width 10
button .btnMigie    -bg gray -text {右へ}       -command "px 3" -width 10
button .btnKonosaki -bg gray -text {この先}     -command "px 4" -width 10
button .btnKouji    -bg gray -text {工事中}     -command "px 5" -width 10
button .btnCyuui    -bg gray -text {注意マーク} -command "px 6" -width 10
button .btnYahidari -bg gray -text {矢印 左}   -command "px 7" -width 10
button .btnYamigi   -bg gray -text {矢印 右}   -command "px 8" -width 10
button .btnZhidari  -bg gray -text {ゼブラ 左} -command "px 9" -width 10
button .btnZmigi    -bg gray -text {ゼブラ 右} -command "px 10" -width 10

# window area placing
grid .lblZenpo    -column 0 -row  1
grid .lblHidarie  -column 0 -row  2
grid .lblMigie    -column 0 -row  3
grid .lblKonosaki -column 0 -row  4
grid .lblKouji    -column 0 -row  5
grid .lblCyuui    -column 0 -row  6
grid .lblYahidari -column 0 -row  7
grid .lblYamigi   -column 0 -row  8
grid .lblZhidari  -column 0 -row  9
grid .lblZmigi    -column 0 -row 10

grid .btnZenpo    -column 1 -row  1
grid .btnHidarie  -column 1 -row  2
grid .btnMigie    -column 1 -row  3
grid .btnKonosaki -column 1 -row  4
grid .btnKouji    -column 1 -row  5
grid .btnCyuui    -column 1 -row  6
grid .btnYahidari -column 1 -row  7
grid .btnYamigi   -column 1 -row  8
grid .btnZhidari  -column 1 -row  9
grid .btnZmigi    -column 1 -row 10

grid .lblPlist -column 2 -row 2
grid .edtPlist -column 2 -row 3

grid .btnClear -column 3 -row 1
grid .btnStore -column 3 -row 2
grid .btnEXIT  -column 3 -row 11



目次

inserted by FC2 system