Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl/TK Main Window Destroy

by Anonymous Monk
on Jul 17, 2003 at 13:07 UTC ( [id://275202]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to set a time on the main window in a Perl/TK application which detroys the window when that time is up. The purpose of the window is to just warn the user that something is about to happen, if they don't stick around to see the warning, it no longer applies, so the window should kill itself after a delay.
I tried adding:
sleep(60); exit(0);

after mainloop();, but that obviously did not work. Any Ideas?

Replies are listed 'Best First'.
Re: Perl/TK Main Window Destroy
by converter (Priest) on Jul 17, 2003 at 13:56 UTC

    Tk is an event-driven GUI toolkit, and provides numerous ways to hook into the event loop. You might want to use the after method (documented in the Tk::after man page). Example:

    #!/usr/bin/perl use Tk; $timeout = 8000; $m = tkinit; # prevent pack() sizing the Toplevel down so # that we can still reach the controls $m->packPropagate(0); $m->Label( -text =>'hi', )->pack; $m->Button( -text=>'Ok', -command => sub { $m->destroy; }, )->pack; # invoke a callback after $timeout msecs $after = $m->after( $timeout, [$m, 'destroy'], ); MainLoop;

    conv

Re: Perl/TK Main Window Destroy
by hiseldl (Priest) on Jul 17, 2003 at 13:47 UTC

    Warning! Tk defines its own exit routine, Tk::exit. If you are exiting from within a Tk widget, you should use Tk::exit. On the other hand, if your process is a forked child process, it should use CORE::exit instead.

    HTH.

    --
    hiseldl
    What time is it? It's Camel Time!

Re: Perl/TK Main Window Destroy
by Tanalis (Curate) on Jul 17, 2003 at 13:22 UTC
    You can set an alarm, and then trap that alarm to exit the application. Something like
    $SIG{ALRM} = sub { exit 0 }; # trap an alarm and assign a sub to exit alarm 60; # trigger an alarm after 60 seconds MainLoop;
    should do the trick.

    Hope that helps.

    -- Foxcub
    #include www.liquidfusion.org.uk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://275202]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found