2016-08-16 46 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

按鈕回調在全球背景處理,因爲按鈕不知道ITCL類和回調發生在這個時候該類不在執行堆棧上。這意味着回調需要使用的,允許外部代碼調用該方法的形式之一:

# 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

好了 - 所以「本」的伎倆

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