getOpenFile and getSaveFile

The getOpenFile and getSaveFile widgets are used, respectively, to bring up dialog boxes to open and save a file. This is typically done in a File menu item. The basic usage is
my $open = $mw->getOpenFile( [ option => value ] );
my $save = $mw->getSaveFile( [ option => value ] );
where the available options include As an example of the use of these two widgets, we adapt an earlier menu example ( containing an Open and Save menu item to use the getOpenFile and getSaveFile widgets.
# file
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
my $menubar = $mw->Menu(-type => 'menubar');
$mw->configure(-menu => $menubar);
my $mfile = $menubar->cascade(-label => '~File', -tearoff => 0);
$mfile->command(-label => '~Open',
                -accelerator => 'Control+o',
                -command => \&open_file);
$mfile->command(-label => '~Save',
                -accelerator => 'Control+s',
                -command => \&save_file);
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$mw->bind('<Control-o>', [\&open_file]);
$mw->bind('<Control-s>', [\&save_file]);
my $types = [ ['Perl files', '.pl'],
              ['All Files',   '*'],];

sub open_file {
  my $open = $mw->getOpenFile(-filetypes => $types,
                              -defaultextension => '.pl');
  print qq{You chose to open "$open"\n} if $open;

sub save_file {
  my $save = $mw->getSaveFile(-filetypes => $types, 
                             -initialfile => 'test',
                             -defaultextension => '.pl');
  print qq{You chose to save as "$save"\n} if $save;
When the user selects the Open menu item, the following dialog box is brought up:

Figure 3.27: Example of a getOpenFile widget
Image open

whereas the following

Figure 3.28: Example of a getSaveFile widget
Image open

is brought up for the Save menu item.

Randy Kobes 2003-11-17