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

Dear kind monks,
If I create an Exit button, I can callback a DialogBox for an exit confirmation easily . However, how do we ask Perl Tk to call the DialogBox if the users choose to exit by either:

- clicking the "x" in the top right corner of the window?
- Or going to File->Close.

Thanks,
Danny

  • Comment on How to call back when exiting the main window

Replies are listed 'Best First'.
Re: How to call back when exiting the main window
by jdporter (Paladin) on Oct 09, 2007 at 20:36 UTC

    You can handle the WM_DELETE_WINDOW protocol:

    use Tk; use Tk::Dialog; my $mw = new MainWindow; $mw->Button( -text => "Exit", -command => \&confirm_exit )->pack; $mw->protocol( WM_DELETE_WINDOW => \&confirm_exit ); my $exit_dlg = $mw->Dialog( -text => "Really exit?", -buttons => [qw(Y +es No)], -default_button => 'Yes' ); MainLoop; warn "exited MainLoop\n"; sub confirm_exit { my $yn = $exit_dlg->Show; $yn =~ /y/i or return; warn "destroying MainWindow\n"; $mw->destroy; }
    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      Thanks monk jdporter, it works!
      Much appreciate it.
      Danny.