当前位置: 代码迷 >> 综合 >> Perl/Tk 变量追踪及类线程实现
  详细解决方案

Perl/Tk 变量追踪及类线程实现

热度:7   发布时间:2024-01-17 11:57:33.0

变量追踪:

某一变量的变化触发回调。

#!/usr/bin/perl -w

use strict;
use Tk::widgets qw/  Trace /;

my $variable = 1;

$MW = Tk::MainWindow->new;

$MW->traceVariable(\$variable , 'w' => [\&callbackfunction]);

Tk::MainLoop;

sub callbackfunction

{

    print "====the variable is changed\n";

}

类线程的实现:

在callbackfunction函数中循环,做你想做的事情,然后调用$MW->update,执行另外的事件,可以达到类似线程的效果。

sub callbackfunction

{

    print "====the variable is changed\n";

    while(1){

       print "to do you want to";

       $MW->update; 

   }

}