0

我试图将方法附加到命令按钮,但收到以下错误消息。如果我附加一个proc,它工作正常。

这个怎么做?

% 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)
4

2 回答 2

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 }]
于 2016-08-17T05:20:03.897 回答
0

好的 - 所以“这个”可以解决问题

% itcl::class a { 
    method test {} {puts test}
    constructor {} {
        button .t.b -command "$this test"; 
        grid config .t.b -column 0 -row 0
    } 
}
于 2016-08-16T18:03:57.210 回答