2010-08-31 50 views
0

我想向腳本添加一個新窗口小部件,該窗口小部件將打開一個帶有文本的新窗口,並在1秒後自動關閉。創建新的perl/tk窗口,將在1秒後自動關閉

我該怎麼辦?

+0

你試過[perldoc -f alarm](http://perldoc.perl.org/functions/alarm.html)嗎? – Dummy00001 2010-08-31 11:35:27

+1

@ Dummy00001在這種情況下,'alarm'函數不是一個好主意; 'Tk'對於這類事情有它自己的事件循環。 – 2010-08-31 12:16:13

回答

1

我想你想要的是Tk::after

#!/usr/bin/perl 

use strict; 
use warnings; 

use Tk; 

my $mw = MainWindow->new; 
my $spawn = $mw->Button(
    -text => 'spawn', 
    -command => sub { 
     my $subwindow = MainWindow->new; 
     my $label  = $subwindow->Label(-text => "spawned"); 
     $label->pack; 
     $subwindow->after(1_000, sub { $subwindow->destroy; }); 
    } 
); 
$spawn->pack; 
my $exit = $mw->Button(
    -text => 'exit', 
    -command => sub { print "exiting...\n"; exit } 
); 
$exit->pack; 

MainLoop;