How to Do a Quick X11 Dialogue Box in Perl
Dialog boxes are used in computer applications to communicate information to users, or for interacting with users. A typical example of a dialog box is a pop-up window that appears when you click a button or perform an action while using a program. These usually have a message and an “OK” and “Cancel” button you click to pass your consent, or lack thereof, back to the application. The X-Windows operating system (X11) on UNIX supports dialog boxes in Perl through the Tk framework, which is a graphic user interface (GUI) system that extends the functionality of the Perl programming language, simplifying the coding of dialog boxes.
Instructions
-
-
1
Launch your Perl editor application. A simple text editor will suffice, provided you save the file with a “.pl” extension, as this will indicate to the system that the file is a Perl program.
-
2
Type the Perl program header and launch a new program, including the “Tk::DialogBox” module by placing the following lines at the top of the file:
#!/bin/perl -w
use Tk;
use Tk::DialogBox;$my main = MainWindow->new;
-
-
3
Create a simple, quick “yes/no” dialog box by typing the following code into the editor:
$dialog = $main->DialogBox( -title => "Type Your Title Here", -buttons => [ "Yes", "No" ] );
This will give you a dialog box with two buttons, one labelled “Yes” and the other labelled “No."
-
4
Add a message to the button using a label by typing the following code into the editor:
$dialog->add("Type your label/message here", -text => $_[0])->pack;
-
5
Display the dialog box by typing the following code into the editor:
$button_clicked = $dialog->Show();
-
6
Type in any required code for the processing that should take place when the user clicks on the “Yes” or “No” button:
if ($button_clicked eq "Yes") { code if “Yes” option is chosen } elsif ($button_clicked eq "No") { code if “No” option is chosen } else { error handling code goes here }
-
1
References
Resources
- Photo Credit Michael Blann/Lifesize/Getty Images