[ Team LiB ] Previous Section Next Section

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 627. 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 39-1:

Table 39-1. Options to tk_messageBox

-default name

Default button name (e.g., yes)

-icon name

Name: error, info, question, or warning.

-message string

Message to display.

-parent window

Embeds dialog in window.

-title title

Dialog title (UNIX and Windows)

-type type

Type: abortretrycancel, ok, okcancel, retrycancel, yesno, or yesnocancel

File and Directory Dialogs

There are two standard file dialogs, tk_getOpenFile and tk_getSaveFile, and one standard directory dialog, tk_chooseDirectory. The tk_getOpenFile dialog is used to find an existing file, while tk_getSaveFile can be used to find a new file. The tk_chooseDirectory dialog, added in Tk 8.3, allows the user to select a directory, rather than a file. These procedures return the selected file or directory name, or the empty string if the user cancels the operation. These procedures take several options that are listed in Table 39-2:

Table 39-2. Options to the standard file and directory dialogs

-defaultextension ext

Appends ext if an extension is not specified. tk_getOpenFile and tk_getSaveFile only.

-filetypes typelist

typelist defines a set of file types that the user can select to limit the files displayed in the dialog. tk_getOpenFile and tk_getSaveFile only.

-initialdir dir

Lists contents of dir in the initial display. If not provided, then the current working directory is displayed.

-initialfile file

Default file, for tk_getSaveFile only.

-message string

A message to include in the client area of the dialog. (Macintosh, only when Navigation Services are installed.) tk_getOpenFile and tk_getSaveFile only. (Tk 8.3.1)

-multiple

Allows the user to select multiple files, returned as a list. tk_getOpenFile only. (Tk 8.4)

-mustexist boolean

If False (default), the user may specify non-existent directories. tk_chooseDirectory only.

-parent window

Creates the dialog as a child of window. The dialog is displayed on top of its parent window.

-title string

Displays 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 39-3:

Table 39-3. Options to tk_chooseColor

-initialcolor color

Initial color to display.

-parent window

Creates the dialog as an embedded child of window.

-title string

Displays string in the title (UNIX and Windows).

    [ Team LiB ] Previous Section Next Section