August 1998
Intermediate to advanced
800 pages
39h 20m
English
You want to create a dialog box, i.e., a new top-level window with buttons to make the window go away. The dialog box might also have other items, such as labels and text entry widgets for creating a fill-out form. You could use such a dialog box to collect registration information, and you want it to go away when registration is sent or if the user chooses not to register.
For simple jobs, use the Tk::DialogBox widget:
use Tk::DialogBox;
$dialog = $main->DialogBox( -title => "Register This Program",
-buttons => [ "Register", "Cancel" ] );
# add widgets to the dialog box with $dialog->Add()
# later, when you need to display the dialog box
$button = $dialog->Show();
if ($button eq "Register") {
# ...
} elsif ($button eq "Cancel") {
# ...
} else {
# this shouldn't happen
}A DialogBox has two parts: the bottom is a set of buttons, and the
top has the widgets of your choosing. Showing a
DialogBox pops it up and returns the button the user selected.
Example 15.6 contains a complete program demonstrating the DialogBox.
Example 15-6. tksample3
#!/usr/bin/perl -w
# tksample3 - demonstrate dialog boxes use Tk; use Tk::DialogBox; $main = MainWindow->new(); $dialog = $main->DialogBox( -title => "Register", -buttons => [ "Register", "Cancel" ], ); # the top part of the dialog box will let people enter their names, # with a Label as a prompt $dialog->add("Label", -text => "Name")->pack(); $entry = $dialog->add("Entry", -width => 35)->pack(); ...Read now
Unlock full access