Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Tk - intercepting alt-f4 and the like

by strat (Canon)
on Mar 18, 2002 at 12:18 UTC ( [id://152463]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I'm playing around a little bit again with Tk, and got the following problem:

If anybody just presses at the X at the upper right corner of the window or Alt+F4 (i mostly work under Win2k), the Window closes and the program finishes.

#!perl -w use strict; use Tk; my $mw = MainWindow->new(); $mw->OnDestroy( \&ExitApplication ); MainLoop; sub ExitApplication { print ("Exiting\n"); # do some cleanup }
With something like the code above, I could at least do some cleanup before quitting.

What I'm looking for: if somebody presses the X at the top right corner of the window or Alt-F4, I want a MsgBox to pop up and ask "Do you really want to quit (Yes/No)", and if the user presses the Button for "No", the program should continue running. With OnDestroy, I only know a way to quit, but not to go on.

Best regards and thanx in advance,
perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Replies are listed 'Best First'.
Re: Tk - intercepting alt-f4 and the like
by physi (Friar) on Mar 18, 2002 at 12:58 UTC
    What you need is:
    #!perl -w use strict; use Tk; my $mw = MainWindow->new(); $mw->protocol('WM_DELETE_WINDOW', \&ExitApplication); MainLoop; sub ExitApplication { print ("Exiting\n"); # do some cleanup }
    This should work, like you need it
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: Tk - intercepting alt-f4 and the like
by rbc (Curate) on Mar 18, 2002 at 16:15 UTC
    You could also do this ...

    sub END { print "Exitting\n"; #do clean up }

    This should on any OS.
    You might want to do a search on the subs BEGIN
    and END.
Re: Tk - intercepting alt-f4 and the like
by strat (Canon) on Mar 20, 2002 at 14:50 UTC
    Btw: If anybody's interested, the cript with Quit-Query could look like:
    #!perl -w use strict; use Tk; use Tk::Dialog; my $mw = MainWindow->new(); $mw->protocol('WM_DELETE_WINDOW', \&ExitApplication); $mw->bind("<Alt-F4>", \&ExitApplication); # intercept Alt-F4, too MainLoop; sub ExitApplication { # Draw dialog yes|no my $dialog = $mw->Dialog(-text => 'Do you really want to quit?', -bitmap => 'question', -title => 'Quit?', -default_button => 'Yes', -buttons => [qw/Yes No/], ); my $answer = $dialog->Show(); if ($answer =~ /y/i){ # maybe do some cleaning up and exit; } else { # continue } } # ExitApplication

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: Tk - intercepting alt-f4 and the like
by strat (Canon) on Mar 18, 2002 at 16:30 UTC
    Thank you very much, physi, that was exactly what I was looking for.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found