Dialog Boxes

There are several reasons you might want to create a dialog box in Perl/Tk. This section will show you what your choices are and how to configure dialog boxes.

The Dialog Widget

The most basic widget provided in Tk to create a dialog box is Dialog. It’s perfect when you need a quick way to display information or get an answer from a user.

$answer = $mw->Dialog(-title => 'Please Reply', 
   -text => 'Would you like to continue?', 
   -default_button => 'yay', -buttons => [ 'yay', 'nay'], 
   -bitmap => 'question' )->Show(  );
if ($answer eq 'yay') {
  # ... do something ...
}

Figure 23-4 shows the output from our code snippet.

A typical dialog box

Figure 23-4.  A typical dialog box

Instead of calling Show immediately, you can save a reference to the dialog and reuse it throughout your application. The options you can use with a Dialog are as follows:

-bitmap => bitmap

Displays a bitmap to the left of the text in the Dialog. Optional.

-buttons => [ button list ]

An anonymous list of buttons to display on the dialog. Buttons will be displayed in the same order they are listed.

-default_button => button text

Whichever button matches this string will be highlighted as the default button for the Dialog. The string match is case senstive, and if the string doesn’t match anything, there will be no default.

-text => text

Text displayed above the buttons in the Dialog.

-title => title

The title of the dialog ...

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.