15.1. How do I get a Dialog box?

For things like a simple "are you sure?" dialog box you might want to take a look at perl5/Tk/Dialog.pm. This module may be invoked with require Tk::Dialog; etc. - there are much more extensive directions inside the comment fields at the top of the Dialog.pm file itself. The module has a lot of options and has a tutorial driver script in perl5/Tk/demos/dialog. Dialog.pm is also used by the perl5/Tk/demos/widget demo. In particular look at perl5/Tk/demos/widget_lib/dialog1.pl and dialog2.pl for examples of how one makes use of Tk::Dialog. A snippet of a script that uses this module could look like:

    require Tk::Dialog;

    my $mw = MainWindow->new;
    my $D = $mw->Dialog(
                 -title => 'Are you sure?',
                 -text  => "You have requested rm \*\nAre you sure?",
                 -default_button => 'No',
                 -buttons        => ['No','yes']
                       );
    my $choice = $D->Show;  # use Show for Tk-b9.01
# if using Tk-b8:    my $choice = $D->show;

    print " you chose $choice \n";
A question concerning configuration of the Subwidgets on the Dialogs came up recently:
<Greg_Cockerham@avanticorp.com> wrote:
! I want to reconfigure the colors of the Dialog and
! ErrorDialog buttons.  How do I do this?
! Thanks in advance.

   $dialog_widget->configure(-background => 'purple'); 
Since these two widgets are composites you manage them like any 'ol widget. If the default delegate subwidget(s) aren't to your liking you can always get to individual component widgets of the composite via the ->Subwidget() method.

I see these subwidgets:

Dialog
'message' is the label subwidget with the dialog text, and 'bitmap' is the label subwidget showing the dialog bitmap
ErrorDialog
'error_dialog' is Dialog subwidget, 'text' is text subwidget
You can even do things like this:
  $error_dialog->Subwidget('error_dialog')->Subwidget('bitmap')->configure(..);
to "get to" the label widget of the dialog component of the error_dialog widget.....

Be sure to also check out the "dialog" demo.


Previous | Return to table of contents | Next