linux – 即使使用了不同的变量ref,为什么waitVariable调用不是独立的?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 即使使用了不同的变量ref,为什么waitVariable调用不是独立的?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3798字,纯文字阅读大概需要6分钟。
内容图文
我是Tk的新手,我想知道这个问题是否是正常的Tk行为.
简而言之:我有一个Perl / Tk(Tk版本804.028)脚本,该脚本使用两个Tk :: ExecuteCommand(v1.6)小部件.这些对象有一个execute_command方法,该方法使用定义的fileevent回调来读取已执行命令的stdout,并在完成后返回.它通过waitVariable的使用来解决.但似乎两个ExecuteCommand一起启动它们只在较慢返回时才返回.完成之后我可能会立即得到更快的回报.
我做了一个小测试Perl / Tk脚本来演示这个问题:
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::ROText;
my $MAIN = new MainWindow -title => "TEST";
my $text = $MAIN->Scrolled('ROText')->pack(qw/-expand 1 -fill both/);
sub pr { # Write into ROText widget
$text->insert('end', join '', @_); $text->yview('end');
}
pr "Tk version ", Tk->VERSION, "\n";
my @v = (100, 200);
sub doo { # Button callback
my ($rv, $txt) = @_;
pr "B4 wait: $txt, ref=$rv, val=", $$rv, "\n";
$MAIN->waitVariable($rv);
pr "Aft wait: $txt, ref=$rv, val=", $$rv, "\n";
}
$MAIN->Button(-text => 'Do 0', -command => [\&doo, \$v[0], "Do 0" ]
)->pack(qw/-expand 1 -side left -fill both/);
$MAIN->Button(-text => 'Stop 0', -command => [sub {++$v[0]; pr "Stop 0\n";} ]
)->pack(qw/-expand 1 -side left -fill both/);
$MAIN->Button(-text => 'Do 1', -command => [\&doo, \$v[1], "Do 1" ]
)->pack(qw/-expand 1 -side left -fill both/);
$MAIN->Button(-text => 'Stop 1', -command => [sub {++$v[1]; pr "Stop 1\n";} ]
)->pack(qw/-expand 1 -side left -fill both/);
MainLoop();
这将绘制一个ROText小部件和4个按钮([Do 0] [Stop 0] [Do 1] [Stop 1])(参见附图).单击“执行”按钮,调用函数doo,等待指定的标量发生更改.按下“停止”按钮时,变量会发生变化.
如果在[Do 0] [Stop 0] [Do 1] [Stop 1]命令中按下按钮,则输出似乎正常(参见第2-7行).但是如果“任务”并行启动,则只有两个回调都停止时,两个回调才会完成.因此按下[Do 0] [Do 1] [Stop 0] [Stop 1](见第8-13行)中的按钮会产生奇怪的结果(见图).
我对第二次测试的期望是第一个回调函数在按下第一个停止按钮后立即返回.所以我认为输出应该是:
B4 wait: Do 0, ref=SCALAR(0x9970560), val=101
B4 wait: Do 1, ref=SCALAR(0x9970bfc), val=201
Stop 0
Aft wait: Do 0, ref=SCALAR(0x9970560), val=102
Stop 1
Aft wait: Do 1, ref=SCALAR(0x9970bfc), val=202
它在Linux机器上运行.
我错过了什么吗?提前致谢!
UPDATE
为了绕过这个waitVariable问题,我重写了这个小部件来代替使用回调(感谢Tantalus!).现在execute_command立即返回.有两个回调,一个用于取消,一个用于完成.现在通过这些回调通知呼叫者.无论如何,我在某处读到(我现在找不到源代码)在Tk中长时间等待回调并不是一个好主意.新解决方案符合此要求.
谢谢您帮忙!
解决方法:
$widget->waitVariable(\$name)
$widget->waitVisibility
$widget->waitWindow
The tk wait methods wait for one of several things to happen, then it returns without taking any other actions. The return value is always an empty string. waitVariable expects a reference to a perl variable and the command waits for that variable to be modified. This form is typically used to wait for a user to finish interacting with a dialog which sets the variable as part (possibly final) part of the interaction. waitVisibility waits for a change in $widget’s visibility state (as indicated by the arrival of a VisibilityNotify event). This form is typically used to wait for a newly-created window to appear on the screen before taking some action. waitWindow waits for $widget to be destroyed. This form is typically used to wait for a user to finish interacting with a dialog box before using the result of that interaction. Note that creating and destroying the window each time a dialog is required makes code modular but imposes overhead which can be avoided by withdrawing the window instead and using waitVisibility.
While the tk wait methods are waiting they processes events in the normal fashion, so the application will continue to respond to user interactions. If an event handler invokes tkwait again, the nested call to tkwait must complete before the outer call can complete.
强调我的.
内容总结
以上是互联网集市为您收集整理的linux – 即使使用了不同的变量ref,为什么waitVariable调用不是独立的?全部内容,希望文章能够帮你解决linux – 即使使用了不同的变量ref,为什么waitVariable调用不是独立的?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。