我是否总是必须向 Tkx 的 -command 参数提供匿名子例程?
我觉得有点奇怪,在为 Tkx 小部件指定 -command
参数时,我必须匿名包装定义的子例程。
TkDocs 教程 的摘录演示了这一点:
my $cb = $frm->new_ttk__button ( -text => "Calculate",
-command => sub {calculate();} );
sub calculate {
$meters = int(0.3048*$feet*10000.0+.5)/10000.0 || '';
}
为什么我写的时候它不起作用<代码>-命令=> &calculate() 或 -command => \&计算()
?
I find it a bit weird that I have to wrap defined subroutines anonymously when specifying the -command
argument for Tkx widgets.
An excerpt from a TkDocs tutorial demonstrates this:
my $cb = $frm->new_ttk__button ( -text => "Calculate",
-command => sub {calculate();} );
sub calculate {
$meters = int(0.3048*$feet*10000.0+.5)/10000.0 || '';
}
Why doesn't it work when I write -command => &calculate()
or -command => \&calculate()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的语法不太正确。您的示例调用子例程(
&
绕过任何原型)并传递返回值 (&calculate()
) 或对其的引用 (\ &calculate()
) 到-command
选项。您想要分配对子例程本身的引用,可以通过以下方式完成:注意缺少尾随括号。另请注意,您不能以这种方式传递参数。如果你想这样做,你需要将调用包装在匿名子例程中:
或者向选项传递 ARRAY 引用而不是 CODE 引用:
这两种形式之间存在细微差别,如果您使用变量而不是文字,这一点很重要价值。
使用第一种形式对
$x
的更改将在调用命令时可见。在第二种形式下,他们不会;每次调用都会看到创建绑定时的值。两种形式都很有用;您只需要在决定使用哪个时做出良好的判断即可。You don't have the syntax quite right. Your examples call the subroutine (the
&
bypasses any prototypes) and passes either the return value (&calculate()
) or a reference to it (\&calculate()
) to the-command
option. You want to assign a reference to the subroutine itself, which you can do via:Note the lack of trailing parentheses. Also note that you can't pass arguments this way. If you want to do that you need to either wrap the call in an anonymous subroutine:
or pass the option an ARRAY reference instead of a CODE reference:
There's a subtle difference between the two forms that's important if you use a variable instead of a literal value.
Using the first form changes to
$x
will be visible when the command is invoked. Under the second form they won't be; each invocation will see the value at the moment the binding was created. Both forms are useful; you just need to exercise good judgment when deciding which to use.它应该与
-command => 一起使用\&计算
。请注意缺少括号。您给出的示例实际上是执行子例程并将其返回的值分配给
-command
键。有关详细信息,请参阅 perlsub 和 perlref
It should work with
-command => \&calculate
. Note the lack of parentheses.The examples you gave are actually executing the subroutine and assigning the value returned by it to the
-command
key.For more info see perlsub and perlref
Zaid,\&calculate 语法确实有效。您试图使用 \&calculate() 语法。第一个绑定到“对稍后运行的子例程计算的引用”,后者绑定为“对(不带参数的调用计算的返回值,并绕过参数原型)的引用。
您是否有更长的、可编译的代码片段可供我们用于测试/分析?我最近对TK不太熟悉。
Zaid, the \&calculate syntax does work. You were trying to use the \&calculate() syntax. The first binds to "a reference to the subroutine calculate to be run later", the latter binds as "a reference to (the return value of a call calculate with no arguments, and bypassing argument prototypes ).
Do you have a longer, compilable code snippet that we can use for testing/analysis? I am not recently familiar with TK.