Method: Test1#initialize

Defined in:
sample/tcltklib/sample1.rb

#initializeTest1

初期化(インタプリタを生成してウィジェットを生成する).



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'sample/tcltklib/sample1.rb', line 17

def initialize()

  #### 使う前のおまじない

  # インタプリタの生成.
  ip = TclTkInterpreter.new()
  # コマンドに対応するオブジェクトを c に設定しておく.
  c = ip.commands()
  # 使用するコマンドに対応するオブジェクトは変数に入れておく.
  append, bind, button, destroy, incr, info, label, place, set, wm =
    c.values_at(
    "append", "bind", "button", "destroy", "incr", "info", "label", "place",
    "set", "wm")

  #### tcl/tk のコマンドに対応するオブジェクト(TclTkCommand)の操作

  # 実行する時は, e() メソッドを使う.
  # (以下は, tcl/tk における info command r* を実行.)
  print info.e("command", "r*"), "\n"
  # 引数は, まとめた文字列にしても同じ.
  print info.e("command r*"), "\n"
  # 変数を用いなくとも実行できるが, 見ためが悪い.
  print c["info"].e("command", "r*"), "\n"
  # インタプリタのメソッドとしても実行できるが, 効率が悪い.
  print ip.info("command", "r*"), "\n"

  ####

  # 以下, 生成したオブジェクトは変数に代入しておかないと
  # GC の対象になってしまう.

  #### tcl/tk の変数に対応するオブジェクト(TclTkVariable)の操作

  # 生成と同時に値を設定する.
  v1 = TclTkVariable.new(ip, "20")
  # 読み出しは get メソッドを使う.
  print v1.get(), "\n"
  # 設定は set メソッドを使う.
  v1.set(40)
  print v1.get(), "\n"
  # set コマンドを使って読み出し, 設定は可能だが見ためが悪い.
  # e() メソッド等の引数に直接 TclTkObject や数値を書いても良い.
  set.e(v1, 30)
  print set.e(v1), "\n"
  # tcl/tk のコマンドで変数を操作できる.
  incr.e(v1)
  print v1.get(), "\n"
  append.e(v1, 10)
  print v1.get(), "\n"

  #### tcl/tk のウィジェットに対応するオブジェクト(TclTkWidget)の操作

  # ルートウィジェットを取り出す.
  root = ip.rootwidget()
  # ウィジェットの操作.
  root.e("configure -height 300 -width 300")
  # タイトルを付けるときは wm を使う.
  wm.e("title", root, $0)
  # 親ウィジェットとコマンドを指定して, ウィジェットを作る.
  l1 = TclTkWidget.new(ip, root, label, "-text {type `x' to print}")
  # place すると表示される.
  place.e(l1, "-x 0 -rely 0.0 -relwidth 1 -relheight 0.1")
  # コマンド名は文字列で指定しても良いが, 見ためが悪い.
  # (コマンド名は独立した引数でなければならない.)
  l2 = TclTkWidget.new(ip, root, "label")
  # ウィジェットの操作.
  l2.e("configure -text {type `q' to exit}")
  place.e(l2, "-x 0 -rely 0.1 -relwidth 1 -relheight 0.1")

  #### tcl/tk のコールバックに対応するオブジェクト(TclTkCallback)の操作

  # コールバックを生成する.
  c1 = TclTkCallback.new(ip, proc{sample(ip, root)})
  # コールバックを持つウィジェットを生成する.
  b1 = TclTkWidget.new(ip, root, button, "-text sample -command", c1)
  place.e(b1, "-x 0 -rely 0.2 -relwidth 1 -relheight 0.1")
  # イベントループを抜けるには destroy.e(root) する.
  c2 = TclTkCallback.new(ip, proc{destroy.e(root)})
  b2 = TclTkWidget.new(ip, root, button, "-text exit -command", c2)
  place.e(b2, "-x 0 -rely 0.3 -relwidth 1 -relheight 0.1")

  #### イベントのバインド
  # script の追加 (bind tag sequence +script) は今のところできない.
  # (イテレータ変数の設定がうまくいかない.)

  # 基本的にはウィジェットに対するコールバックと同じ.
  c3 = TclTkCallback.new(ip, proc{print("q pressed\n"); destroy.e(root)})
  bind.e(root, "q", c3)
  # bind コマンドで % 置換によりパラメータを受け取りたいときは,
  # proc{} の後ろに文字列で指定すると,
  # 置換結果をイテレータ変数を通して受け取ることができる.
  # ただし proc{} の後ろの文字列は,
  # bind コマンドに与えるコールバック以外で指定してはいけない.
  c4 = TclTkCallback.new(ip, proc{|i| print("#{i} pressed\n")}, "%A")
  bind.e(root, "x", c4)
  # TclTkCallback を GC の対象にしたければ,
  # dcb() (または deletecallbackkeys()) する必要がある.
  cb = [c1, c2, c3, c4]
  c5 = TclTkCallback.new(ip, proc{|w| TclTk.dcb(cb, root, w)}, "%W")
  bind.e(root, "<Destroy>", c5)
  cb.push(c5)

  #### tcl/tk のイメージに対応するオブジェクト(TclTkImage)の操作

  # データを指定して生成する.
  i1 = TclTkImage.new(ip, "photo", "-file maru.gif")
  # ラベルに張り付けてみる.
  l3 = TclTkWidget.new(ip, root, label, "-relief raised -image", i1)
  place.e(l3, "-x 0 -rely 0.4 -relwidth 0.2 -relheight 0.2")
  # 空のイメージを生成して後で操作する.
  i2 = TclTkImage.new(ip, "photo")
  # イメージを操作する.
  i2.e("copy", i1)
  i2.e("configure -gamma 0.5")
  l4 = TclTkWidget.new(ip, root, label, "-relief raised -image", i2)
  place.e(l4, "-relx 0.2 -rely 0.4 -relwidth 0.2 -relheight 0.2")

  ####
end