Tcl/Tk - 无法将 class 方法附加为按钮命令
Tcl/Tk - Unable to attach class method as button command
我试图将方法附加到命令按钮,但收到以下错误消息。如果我附加一个 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)
好的 - 所以 "this" 成功了
% itcl::class a {
method test {} {puts test}
constructor {} {
button .t.b -command "$this test";
grid config .t.b -column 0 -row 0
}
}
按钮回调在 全局 上下文中处理,因为按钮不知道 itcl classes 并且回调发生在 class 不在执行堆栈上。这意味着回调需要使用允许外部代码调用方法的形式之一:
# 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 }]
我试图将方法附加到命令按钮,但收到以下错误消息。如果我附加一个 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)
好的 - 所以 "this" 成功了
% itcl::class a {
method test {} {puts test}
constructor {} {
button .t.b -command "$this test";
grid config .t.b -column 0 -row 0
}
}
按钮回调在 全局 上下文中处理,因为按钮不知道 itcl classes 并且回调发生在 class 不在执行堆栈上。这意味着回调需要使用允许外部代码调用方法的形式之一:
# 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 }]