$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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |