You can do something like this in the button definition:
$QuitButton = $TopFrame->Button( -text => 'Quit', -relief => 'raised', -cursor => 'pirate', -command => sub { my $str = "Are you sure you want to Quit?"; if ( &GUI_PopupQuery("TOOL","QUIT","Cancel",$str )) { ## add code here to close any open windows... $MW->destroy(); } });

You need then a subroutine to create a popup window, and receive the arguments passed above, and then return either 1 or 0, depending on the results of the the query. Note that you should probably do additional things, like make the popup do a toplevel grab, so that it has grabbed the input focus, bind <RETURN> to the default function, which would be NO presumably, then make the toplevel do a wait...

Here's a stripped down version of this that could work. In my code, I pass the affirmative and negative response strings to the generic popup subroutine, so I can call it with a variety of queries. This is pared down code from the actual source that I copied it from, so hopefully it's sufficient in itself, but you may need to add tweaks here and there, for padding, fonts, window features, etc. The toplevel window is in $topLevel:

sub GUI_PopupQuery { my ($title, $AffirmativeText, $NegativeText, $str) = @_; print("\a"); ## beep my ($topLevel, $topFrame, $botFrame, $labelFrame, $CancelButton, $OKButton); $topLevel = $MW->Toplevel(); $topLevel->wm('transient', $MW); $topLevel->geometry('+200+200'); $topLevel->title("$title"); $topLevel->resizable(1, 0); ## FRAMES... $topFrame = $topLevel->Frame(-height => 120, -width => 150, -relief => 'ridge', )->pack(-side => 'top', -fill => 'both', -expand => 1); $botFrame = $topLevel->Frame(-height => 120, -width => 150, -relief => 'ridge', -borderwidth => 2, )->pack(-side => 'bottom', -fill => 'both', -expand => 1); ## TOP FRAME ELEMENTS... $topFrame->Frame( -height => 20, )->pack(-side => 'top', -pady => 5, -fill => 'both', -expand => 1); $labelFrame = $topFrame->Frame(-height => 50, -width => 50, -relief => 'flat', -borderwidth => 2, )->pack(-side => 'top', -fill => 'both', -expand => 1, -pady => 5); $labelFrame->Label(-text => $str, -width => 60, -relief => 'groove', -padx => 6, -pady => 6, -bg => 'red3', -fg => 'white', )->pack( -anchor => 'center', -fill => 'both', -expand => 1); ## BOTTOM FRAME ELEMENTS... my $botButtonFrame = $botFrame->Frame(-height => 120, -width => 150, -relief => 'ridge', -borderwidth => 2, )->pack(-side => 'bottom'); $OKButton = $botButtonFrame->Button(-text => $AffirmativeText, -padx => 8, -pady => 8, -width => 8, -command => sub { $returnval=1; $topLevel->grabRelease; $topLevel->destroy(); }, -font => $fontB )->pack(-side =>'left'); $CancelButton = $botButtonFrame->Button(-text => $NegativeText, -padx => 8, -pady => 8, -width => 8, -command => sub { $returnval=0; $topLevel->grabRelease; $topLevel->destroy(); }, )->pack(-side => 'left'); # Bind the "OK" button to exit when "<return>" # is pressed $topLevel->bind("<Return>", sub{ $topLevel->destroy(); $returnval=1; } ); $topLevel->waitVisibility(); $topLevel->waitWindow(); return $returnval; }

Hope this may be useful for you...

Cheers!
Cadphile...


In reply to Re: Cancel close of mainwindow in Tk on Windows by cadphile
in thread Cancel close of mainwindow in Tk on Windows by smaclach

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.