2016-08-16 3 views
0

メソッドをコマンドボタンにアタッチしようとしましたが、次のエラーメッセージが表示されました。私はprocを添付すれば正常に動作します。Tcl/Tk - クラスメソッドをボタンコマンドとしてアタッチできません

これを行う方法?

% itcl::class a { 
    method test {} {puts test} 
    constructor {} { 
     button .t.b -command test; 
     grid config .t.b -column 0 -row 0 
    } 
} 

% a A 

invalid command name "resize" 
invalid command name "resize" 
    while executing 
"resize" 
    invoked from within 
".t.b invoke" 
    ("uplevel" body line 1) 
    invoked from within 
"uplevel #0 [list $w invoke]" 
    (procedure "tk::ButtonUp" line 24) 
    invoked from within 
"tk::ButtonUp .t.b" 
    (command bound to event) 
+0

は、それは 'test'や' resize'ですか? –

+0

同じものを修正しました – BabyGroot

答えて

2

ボタンのコールバックがグローバルコンテキストで処理されないので、クラスは実行スタック上にありません。これは、コールバックは、外部のコードがメソッドを呼び出すことができますフォームのいずれかを使用する必要があることを意味します

# The form with "$this test" is not preferred as it can go wrong with complex data. 
# It tends to show up when you tidy things up for a demo! Using [list] avoids trouble. 
button .t.b -command [list $this test] 
# The [namespace code] command picks up the current namespace context. That's what itcl 
# needs to work correctly. 
button .t.b -command [namespace code { test }] 
0

OK - ボタンはITCLクラスを知らないとコールバックをした時点で起こるので、「これは」トリック

% itcl::class a { 
    method test {} {puts test} 
    constructor {} { 
     button .t.b -command "$this test"; 
     grid config .t.b -column 0 -row 0 
    } 
} 
関連する問題