Perl Tk标签更改背景颜色与textvariable相关联

时间:2015-01-28 17:16:46

标签: perl text label background-color tk

如何自动更改与textvariable关联的标签背景颜色? 即当textvariable为正值时,背景变为绿色。当它是负值时,它会自动变为红色。请指教。

use strict;
use Tk;
my $mw = new MainWindow(-title => "TTTT",
 );
$mw->geometry( "400x300" );

my $LAST1 ;
my $bgclr = 'green';
my $label1 = $mw->Label ( -textvariable=>\$LAST1, -fg=>'White',
-font=> 'arial 20',
-bg => $bgclr,
);

$label1->place(-x=>10, -y => 65);

$mw->repeat(2000,\&tick);
MainLoop;


sub tick {

my $AA= '9.16';
my $sum = '235.72';
my $avge = (($sum + $AA) / 25);
my $RR = ((($AA - $avge) / $avge) *100);
$LAST1 = $RR;
}

1 个答案:

答案 0 :(得分:0)

我建议在大型程序中使用Treads和Tread :: Shared,以便在运行外部子服务器时更新主GUI。 但是对于你的例子来说,这可能就足够了。

以下是您提供的代码的示例代码:

use strict;
use Tk;
our $time_tick = 1;
our $label1;
our $Pos_Color = 'green';
our $Neg_Color = 'red';
my $mw = new MainWindow(-title => "TTTT",
 );
$mw->geometry( "400x300" );

my $LAST1 ;
my $bgclr = $Pos_Color;
$label1 = $mw->Label ( -textvariable=>\$time_tick, -fg=>'White',
-font=> 'arial 20',
-bg => $bgclr,
);

$label1->place(-x=>10, -y => 65);

$mw->repeat(2000,\&tick);
MainLoop;


sub tick {
    $time_tick = $time_tick * -1;
    if ($time_tick > 0)
    {
        $label1->configure(-bg => $Pos_Color);
    }
    else
    {
        $label1->configure(-bg => $Neg_Color);  
    }
}