Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to exit the mainwindow after 5 seconds from the program is started. if used didn't click the ok or calcel button I want to take the default value cancel. how can I achive this. in the following code.
my $mw = new MainWindow(-height => 600,-width => 600 ,-title=>"Message +"); # Main Window $mw->overrideredirect(1); # Remove window $mw->geometry('+700+650'); my $frm_name = $mw -> Frame() -> pack(); my $lab; if ($ARGV[0] eq "-A") { $lab = $frm_name -> Label( -text=>"D[$ARGV[1]]\nClick 'Ok' To start the Process", -anchor => 'center',-height => 5,-width=> 55 , -foreground => 'blue', ) -> pack(); $mw -> Button(-text=>"Ok", -command =>sub { my $status = $mw -> messageBox(-message=>"Do u want to start the +process..?",-type=>'yesno',); if($status eq "Yes") { print "Yes"; exit; }else{ ; } } ) -> pack(-side => 'right',-expand => 0); $mw -> Button(-text=>"Cancel", -command =>sub { print"cancel"; exit;}) + -> pack(-side =>'right',-expand => 0 ); } MainLoop;

Replies are listed 'Best First'.
Re: Perl Tk exit the main window after 5 seconds...
by zentara (Cardinal) on Aug 09, 2010 at 11:48 UTC
    Use a timer to destroy the mainwindow. If you need the script to continue on, destroy the mainwindow, instead of exiting, then you can run more commands after the Tk window is gone, leaving you with the default answer.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $choice; my $mw = new MainWindow(-height => 600,-width => 600 ,-title=>"Message +"); # Main Window $mw->overrideredirect(1); # Remove window manager controls $mw->geometry('+700+650'); my $frm_name = $mw -> Frame() -> pack(); my $lab; if ($ARGV[0] eq "-A"){ $lab = $frm_name -> Label( -text=>"D[$ARGV[1]]\nClick 'Ok' To start the Process", -anchor => 'center',-height => 5,-width=> 55 , -foreground => 'blue', ) -> pack(); $mw -> Button(-text=>"Ok", -command =>sub { my $status = $mw -> messageBox( -message=>"Do u want to start the process..?", -type=>'yesno',); if($status eq "Yes") { $choice = "Yes"; print "$choice\n"; exit; }else{ $choice = "No"; } } ) -> pack(-side =>'right',-expand => 0); $mw -> Button(-text=>"Cancel", -command =>sub { print"cancel"; exit;}) + -> pack(-side =>'right',-expand => 0 ); } my $timer = $mw->after(5000, sub{ $choice = 'No'; print "$choice\n"; $ +mw->destroy; } ); MainLoop; print "after mainloop exits-> $choice\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      Thanks your replay.... I have clicked the ok button then the yes/no dialog box is opened and this program exit with some necessary data? how to destroy the message box also.
        sorry.. unnecessary data.