The New PerlTk.org - File Dialogs
www.mamboteam.com
The New PerlTk.org  
Home
Sunday, 20 April 2014
 
 
Main Menu
Home
Perl/Tk Widgets
Articles
Scripts
Useful Tips
Links
Contact Us
Search
FAQs
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
File Dialogs PDF Print E-mail
Written by Slaven Rezic   
by Slaven Rezic

This month's article will deal with the ability of Perl/Tk to select files and directories. It seems to be state of the art that selecting files is not done within a mere entry widget, but rather with compound widgets showing available files and directories, possible in a tree structure.

getOpenFile/getSaveFile

Image

 The standard methods in Perl/Tk for displaying file dialogs are getOpenFile (for opening existing files) and getSaveFile (for selecting a name for a non-existing file). These methods will use - if possible - a native interface. That is, on Windows the standard file dialog and on Macintosh (if there will be ever a port of Perl/Tk for the Mac) too. In Unix/X11, there is no standard file dialog (note that Tk is built on top of Xlib, so KDE/Qt, CDE/Motif and GNOME/Gtk widgets are not available). In this case, the dialog is a mega widget written in Perl (which, as shown below, can also be used for other platforms).

getOpenFile/getSaveFile is easy to use: just say

 $top->getOpenFile;
or
 $top->getSaveFile;
The execution of the program will stop at this point and a file dialog will be displayed. After selecting Open/Save or Cancel, the method call will return the selected file name or an empty string.

There are a couple of extra options which are described in the Tk::getOpenFile manual page. An example script using more options can be found here. The -filetypes option can be used to specify a number of filters (e.g. show only perl files with the file extension .pl). These filters are displayed in an option menu widget. It is good practice to always include an entry "All files", which matches every file in the current directory. The first entry in the -filetypes argument is the default entry. If -filetypes is omitted, the file dialog will show every file.

The Windows user is usually familiar with the use of the standard file dialog. Using the X11 file dialog is quite similar. There is an additional feature which is especially useful in mouse-less usage. By switching the focus to the icon list (this is done by hitting Shift-Tab once), it is possible to type the wanted file name. While typing, the selection moves to match an appropriate file name. Hitting return will accept the active selection as the return value of the dialog.

Tk::FileSelect

Image

 Back in the old Tk 400.xxx days, there was only the Tk::FileSelect widget available. It's still in the newer Perl/Tk distributions, and it has some limitations, but also its advantages.

First, here is an example for the usage of the FileSelect dialog. This example shows one of the advantages of this widget: you can change the names of the labels, which make it more suitable for internationalisation (in the example the labels were changed to carry german names). The set of label options is not complete (yet), but will hopefully in the future.

You can also specify a -verify option to check if the selected file meets your specifications. The argument is a list of the standard perl file tests (like ['-r', '-T'], to restrict to readable text files). This option can also be used to restrict FileSelect to "DirSelect", by changing the verify option to ['-d'] (here is an example). I will discuss later another way for selecting directories.

The options of Tk::FileSelect are poorly documented, so here is a short overview of possible options:

  • -directory: the directory which will be displayed at start
  • -initialfile: the initially selected file
  • -filter: only the matching files will be shown (pattern is a shell glob)
  • -filelabel, -filelistlabel, -dirlabel, -dirlistlabel: change the labels for i18n
  • -verify: see above
  • -width/-height: the dimension of the directory and file listboxes

The forthcoming versions of Tk::FileSelect will contain -command and -transient options to enable non-dialog use of the widget.

Note that Tk::FileSelect is partially broken in the newer Tk's (that is the beta ones: from 800.016 to 800.018), but this will change in the next releases.

If you want to use Tk::FileSelect as the default file dialog, then you can say

    use Tk::FileSelect 'as_default';
This will cause to change the getOpenFile/getSaveFile methods to use the FileSelect widget from now on.

Tk::Dirlist

Image

 A lesser known widget in the Perl/Tk distribution is Tk::Dirlist. It's a widget displaying a tree of directories and files, beginning from the root to a specified directory (or the current directory). This widget is not a dialog, so you have to do the packing (or gridding) yourself. It also has no bindings to user interaction, but since the widget is derived from Tk::HList, the -command and -browsecmd options can be used. Here is a short but complete example for the use of Tk::Dirlist.

Tk::FBox

It is also possible to use Tk::FBox, which is the widget serving getOpenFile and getSaveFile behind the scenes. Tk::FBox can be configured with all the options getOpenFile takes and the additional option -type, which should be either open or save. After creating the widget, it can be shown with the Show method, as nearly every Perl/Tk dialog. Using Tk::FBox directly is probably useless on X11, but another file dialog alternative on Win32. Here is an example usage of Tk::FBox.

Dialog for directories: Tk::DirTree

Image

 One way for selecting directories was already discussed (the -d verify option of Tk::FileSelect). In the Perl/Tk distribution, there is another widget specifically dealing with directories: Tk::DirTree. This widget is not a dialog, so you have to create a toplevel window and embed this widget in it, and also use waitVariable to block execution until the user made a choice (Note: waitVariable waits until the referenced variable changes its value. While waiting, the event mechanism of Tk is not blocked, so you can click on other buttons etc.). In the example script this usage is demonstrated. The widget is configured to change the directory with a single click and to select a directory with a double click (or by clicking the Ok button which will use the last selected directory). If you don't like this behaviour, you can easily change the code so that double-clicks open the selected directory, and the Ok button is the only way to confirm a selection. This is also described in the sample script.

More on CPAN

If you're still not satisfied with these alternatives, there are more choices on CPAN: Tk::FileDialog, which, by author's taste, looks nicer than Tk::FileSelect and Tk::SelFile. The Tk:FileEntry widget is a combination of an entry with a button, which pops up a file dialog on request (this is similar to the HTTP upload button in HTML forms, as it is used in the Netscape browser).

Comments
perl/tk
Written by yassinovski on 2006-10-10 04:29:18
i wante to print the tree of Dirtree widget in a texte file
perl/tk
Written by yassinovski on 2006-10-10 04:32:28
returns a content of Dirtree as texte file

Only registered users can write comments.
Please login or register.

Powered by AkoComment 2.0!

< Prev   Next >
 
Top! Top!