Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 36.  Focus, Grabs, and Dialogs

Standard Dialogs

The tk_dialog command presents a choice of buttons and returns a number indicating which one was clicked by the user. The general form of the command is:

tk_dialog win title text bitmap default ?label? ?label? ...

The title appears in the title bar, and the text appears in the dialog. The bitmap appears to the left of the text. Specify {} for the bitmap if you do not want one. The set of built-in bitmaps is given on page 541. The label arguments give labels that appear on buttons along the bottom of the dialog. The default argument gives the index of the default button, counting from zero. If there is no default, specify {} or -1.

Message Box

The tk_messageBox dialog is a limited form of tk_dialog that has native implementations on the different platforms. Like tk_dialog, it allows for a message, bitmap, and a set of buttons. However, the button sets are predefined, and the bitmaps are limited. The yesno button set, for example, displays a Yes and a No button. The abortretryignore button set displays Abort, Retry, and Ignore buttons. The tk_messageBox command returns the symbolic name of the selected button (e.g., yes or retry.) The yesnocancel message box could be used when trying to quit with unsaved changes:

set choice [tk_messageBox -type yesnocancel -default yes \
    -message "Save changes before quitting?" \
    -icon question]

The complete set of options to tk_messageBox is listed in Table 36-1:

Table 36-1. Options to tk_messageBox.
-default nameDefault button name (e.g., yes)
-icon nameName: error, info, question, or warning.
-message stringMessage to display.
-parent windowEmbeds dialog in window.
-title titleDialog title (UNIX and Windows)
-type typeType: abortretrycancel, ok, okcancel, retrycancel, yesno, or yesnocancel

File Dialogs

There are two standard file dialogs, tk_getOpenFile and tk_getSaveFile. The tk_getOpenFile dialog is used to find an existing file, while tk_getSaveFile can be used to find a new file. These procedures return the selected file name, or the empty string if the user cancels the operation. These procedures take several options that are listed in Table 36-2:

Table 36-2. Options to the standard file dialogs.
-defaultextension extAppends ext if an extension is not specified.
-filetypes typelisttypelist defines a set of file types that the user can select to limit the files displayed in the dialog.
-initialdir dirLists contents of dir in the initial display.
-initialfile fileDefault file, for tk_getSaveFile only.
-parent windowCreates the dialog as an embedded child of window.
-title stringDisplays string in the title (UNIX and Windows).

The file dialogs can include a listbox that lists different file types. The file types are used to limit the directory listing to match only those types. The typelist option specifies a set of file extensions and Macintosh file types that correspond to a named file type. If you do not specify a typelist, users just see all the files in a directory. Each item in typelist is itself a list of three values:

name extensions ?mactypes?

The name is displayed in the list of file types. The extensions is a list of file extensions corresponding to that type. The empty extension "" matches files without an extension, and the extension * matches all files. The mactypes is an optional list of four-character Macintosh file types, which are ignored on other platforms. On the Macintosh, if you give both extensions and mactypes, the files must match both. If the extensions is an empty list, only the mactypes are considered. However, you can repeat name in the typelist and give extensions in one set and mactypes in another set. If you do this, then files that match either the extensions or mactypes are listed.

The following typelist matches Framemaker Interchange Files that have both a .mif extension and a MIF type:

set typelist {
   {"Maker Interchange Files" {".mif"} {"MIF "}}

The following typelist matches GIF image files that have either a .gif extension or the GIFF file type. Note that the mactypes are optional:

set typelist {
   {"GIF Image" {".gif"}}
   {"GIF Image" {} {"GIFF"}}}

The following typelist puts all these together, along with an entry for all files. The entry that comes first is displayed first:

set typelist {
   {"All Files" {*}}
   {"GIF Image" {".gif"}}
   {"GIF Image" {} {"GIFF"}}
   {"Maker Interchange Files" {".mif"} {"MIF "}}

Color Dialog

The tk_chooseColor dialog displays a color selection dialog. It returns a color, or the empty string if the user cancels the operation. The options to tk_chooseColor are listed in Table 36-3:

Table 36-3. Options to tk_chooseColor.
-initialcolor colorInitial color to display.
-parent windowCreates the dialog as an embedded child of window.
-title stringDisplays string in the title (UNIX and Windows).

      Previous section   Next section