http://www.perlmonks.org?node_id=525421

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

Hello Monks, Can anyone tell me how to run code BEFORE a main window is destroyed in Tk. Basically I want to clean up my temp files when the user quits my app. OnDestroy doesn't seem to be the way to do it Thanks all

Replies are listed 'Best First'.
Re: Tk before destroy event
by rinceWind (Monsignor) on Jan 25, 2006 at 12:08 UTC

    Not sure what you mean by OnDestroy. The standard way of doing this is to bind a <Destroy> event on a passive widget (such as a label) on your main window. Don't bind the main window itself, as all subwidgets will inherit the binding; that way leads to much confusion and many bugs.

    If you don't mind the cleanup happening after the main window is destroyed, you can always put the cleanup code after MainLoop, so it won't get called until MainLoop returns.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      Yes. Thanks, I've got it now.
Re: Tk before destroy event
by strat (Canon) on Jan 26, 2006 at 09:03 UTC

    Maybe the following piece of code might be interesting for you:

    use Tk (); use Tk::Dialog (); my $mw = MainWindow->new(); $mw->protocol('WM_DELETE_WINDOW', \&ExitApplication); # ------------------------------------------------------------ sub ExitApplication { my $dialog = $mw->Dialog(-text => 'Programm wirklich beenden?', -bitmap => 'question', -title => 'Programm beenden', -default_button => 'Yes', -buttons => [qw/Yes No/], ); my $answer = $dialog->Show(); # and display dialog if (lc($answer) eq 'yes'){ # perhaps save data if necessary... exit; } else { # continue } # else } # ExitApplication

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"