Hi Graham Drabble,

It's true that you can use OnDestroy to invoke some code when a widget is destroyed, but only if it's not the MainWindow.  If it's the MainWindow that's destroyed, it's too late, as you've discovered.

Here's a way to test it.  With some slight modifications to your code and the addition of a boolean variable $b_destroy_button you can try running the following:

use strict; use warnings; use Tk; use Tk::DialogBox; # User-defined my $b_destroy_button = 0; # If set, destroy the Button, not the MainW +indow # Main program my $mw = MainWindow->new(); my $button = $mw->Button(-text => 'Exit'); # Choose which widget to destroy my $psub; ($b_destroy_button) or $psub = sub { $mw->destroy() }; ($b_destroy_button) and $psub = sub { $button->destroy() }; $button->configure(-command => $psub); $button->pack(-side => 'right', -ipadx => 10, -padx => 30); $button->OnDestroy(\&exit_app); MainLoop; # Subroutines sub exit_app{ print "1\n"; my $db = $mw->DialogBox(-title => 'Error',-buttons => ['CSV','Aban +don']); my $text = "Do you wish to save a new CSV file or abandon changes? +"; $db->Label(-text => $text)->pack(); my $button = $db->Show(); if ($button eq 'CSV'){ save_csv(); } } sub save_csv{ return 1; }

Now if you run it "as is", you'll get the same behavior, because destroying the MainWindow causes a problem.

But when you destroy just the Button instead, by changing the value of $b_destroy_button to 1, it will successfully give you the DialogBox you want.

An alternative way to do this (if you don't want to have to destroy a widget before taking the action of possibly writing the CSV file), is to just put the call to exit_app() in the callback for the Button.  Then, after you're done writing the CSV file (or not), you can have the callback destroy the application, like so:

use strict; use warnings; use Tk; use Tk::DialogBox; # Main program my $mw = MainWindow->new(); my $button = $mw->Button(-text => 'Exit'); $button->configure(-command => \&exit_app); $button->pack(-side => 'right', -ipadx => 10, -padx => 30); MainLoop; # Subroutines sub exit_app{ print "1\n"; my $db = $mw->DialogBox(-title => 'Error',-buttons => ['CSV','Aban +don']); my $text = "Do you wish to save a new CSV file or abandon changes? +"; $db->Label(-text => $text)->pack(); my $button = $db->Show(); if ($button eq 'CSV'){ save_csv(); } $mw->destroy(); } sub save_csv{ return 1; }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: $mw->OnDestroy and Dialog Box by liverpole
in thread $mw->OnDestroy and Dialog Box by Graham Drabble

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.