- 論壇徽章:
- 0
|
請教各位大俠,我想用tk實現(xiàn)這樣一個功能: 點擊button stop后,button的item顯示為start,同時label中的text停止?jié)L動顯示,然后點擊button start,label的text會繼續(xù)顯示。
以下是我寫的, 發(fā)現(xiàn)在tk中fork時,sleep 沒有生效,而且感覺變量$but改變也沒有生效,如果不用fork直接使用信號,那么就會阻塞,sleep 10s后,button才能改變,而不是按下去就改變,
是什么問題呢?關(guān)于進程、事件驅(qū)動這塊我理解的還不深入,希望學習下。
#!/usr/bin/perl
use TK;
my $mw = MainWindow->new;
my $mess = "this is from me ^o^";
my $but = "Stop";
my ($ppid,$pid);
$SIG{'USR1'} = sub {
sleep(10);
};
$ppid = $$;
$mw->Label(-textvariable => \$mess)->pack(-expand => "1");
$mw->after(100,\&scroll);
$mw->Button(
-textvariable => \$but,
-command => sub {
if($but eq "Start") {
$pid = fork();
if ( $pid ==0 ) {
print "start->stop\n";
} else {
wait;
$but = "Stop";
}
} else {
$pid = fork();
if ( $pid ==0 ) {
system("kill -USR1 $ppid");
print "stop->start\n";
} else {
wait;
$but = "Start";
}
}
}
)->pack;
sub scroll {
$mess =~ /(.)(.*)/;
$mess = "$2$1";
$mw->after(100,\&scroll);
}
$mw->MainLoop; |
|