Previous
Canvas, Message, Adjuster, Scrolled 
Next
Grid, Pack 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Dialogs

Dialogs

Dialogs can be called the elements in a program that detaches itself from the main window. This is a VERY general definition and has many problems. But for the moment, it will do. Tk provides many dialogs.

messageBox

This procedure creates and displays a message window with an application-specified message, an icon and a set of buttons. Each of the buttons in the message window is identified by a unique symbolic name (see the -type options). After the message window is popped up, messageBox waits for the user to select one of the buttons. Push the below button to see an example of messageBox

Some Options
-default=>name Name gives the symbolic name of the default button for this message window ('ok', 'cancel', and so on). See -type for a list of the symbolic names. If this option is not specified, the first button in the dialog will be made the default.
-icon>iconImage Specifies an icon to display. IconImage must be one of the following: error, info, question or warning. If this option is not specified, then the info icon will be displayed.
-message>string Specifies the message to display in this message box.
-title>String Specifies a string to display as the title of the message box. The default value is an empty string.
-type>predefinedType Arranges for a predefined set of buttons to be displayed. The following values are possible for predefinedType:
abortretryignoreDisplays three buttons whose symbolic names are abort, retry and ignore.
okDisplays one button whose symbolic name is ok.
okcancelDisplays two buttons whose symbolic names are ok and cancel.
retrycancelDisplays two buttons whose symbolic names are retry and cancel.
yesnoDisplays two buttons whose symbolic names are yes and no.
yesnocancelDisplays three buttons whose symbolic names are yes, no and cancel.


Example

#!/usr/local/bin/perl
use Tk;
use strict;

# Main Window
my $mw = new MainWindow;
my $button = $mw->Button(-text=>"Show Quit Dailog", -command => \&exitTheApp)->pack();

sub exitTheApp {
	my $response = $mw -> messageBox(-message=>"Really quit?",-type=>'yesno',-icon=>'question');
	
	if( $response eq "Yes" ) {
		exit
	} else {
		$mw -> messageBox(-type=>"ok", -message=>"I know you like this application!");
	}
}

MainLoop;

chooseColor

chooseColor pops up a dialog box for the user to select a color.

Some Options
-initialcolor>COLOUR Specifies the color to display in the color dialog when it pops up.



getOpenFile

The procedures getOpenFile and getSaveFile pop up a dialog box for the user to select a file to open or save. The getOpenFile command is usually associated with the Open command in the File menu. Its purpose is for the user to select an existing file only. If the user enters an non-existent file, the dialog box gives the user an error prompt and requires the user to give an alternative selection. If an application allows the user to create new files, it should do so by providing a separate New menu command.

The getSaveFile command is usually associated with the Save as command in the File menu. If the user enters a file that already exists, the dialog box prompts the user for confirmation whether the existing file should be overwritten or not.

Some Options
-initialdir=>DIRNAME Specifies that the directories in directory should be displayed when the dialog pops up. If this parameter is not specified, then the directories in the current working directory are displayed. If the parameter specifies a relative path, the return value will convert the relative path to an absolute path.
-defaultextension=>EXTENSION Specifies a string that will be appended to the filename if the user enters a filename without an extension. The default value is the empty string, which means no extension will be appended to the filename in any case.
-filetypes=>filePatternList If a File types listbox exists in the file dialog on the particular platform, this option gives the filetypes in this listbox. When the user choose a filetype in the listbox, only the files of that type are listed. If this option is unspecified, or if it is set to the empty list, or if the File types listbox is not supported by the particular platform then all files are listed regardless of their types. This is a little tricky - see manual for information.
-initialfile=>FILENAME Specifies a filename to be displayed in the dialog when it pops up.
-multiple Allows the user to choose multiple files from the Open dialog.



Toplevel

toplevel is a widget. This can be used to create custom dialog boxes. A toplevel is similar to a frame except that it is created as a top-level window: its X parent is the root window of a screen rather than the logical parent from its path name. The primary purpose of a toplevel is to serve as a container for dialog boxes and other collections of widgets. The only visible features of a toplevel are its background color and an optional 3-D border to make the toplevel appear raised or sunken.

One can use toplevel to create new windows. The widgets can be packed inside it in the same way widgets are packed inside a frame. An example...


#!/usr/local/bin/perl
use Tk;
# Main Window
$mw = new MainWindow;

my $lab = $mw -> Label(-text=>"This is the root window.",
		-font=>"ansi 12 bold") -> pack;
my $but = $mw -> Button(-text=>"Click to Create Toplevel", 
		-command=>\&makeTop)  -> pack;

MainLoop;

#A function to make a toplevel window
sub makeTop {
	my $top = $mw -> Toplevel(); #Make the window
	#Put things in it
	my $top_lab = $top -> Label(-text=>"This is the Toplevel window.",
			-font=>"ansi 12 bold") -> pack;
	my $txt = $top -> Text() -> pack;
	$txt -> insert('end', "Widgets can be packed in this window.");
	
	#An option to close the window.
	my $but_close = $top -> Button(-text=>"Close", 
		-command => sub { destroy $top; } ) -> pack;
}
Previous
Canvas, Message, Adjuster, Scrolled 
Next
Grid, Pack 
blog comments powered by Disqus
Subscribe to Feed