Previous
Canvas, Message 
Next
Grid, Pack 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
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.

tk_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, tk_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

set answer [tk_messageBox -message "Really quit?" -type yesno -icon question]
switch -- $answer {
    yes exit
    no {tk_messageBox -message "I know you like this application!" -type ok}
}

tk_chooseColor

tk_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.



tk_chooseDirectory

tk_chooseDirectory command pops up a dialog box for the user to select a directory.

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.
-mustexist BOOLEAN Specifies whether the user may specify non-existent directories. If this parameter is true, then the user may only select directories that already exist. The default value is false.



tk_getOpenFile

The procedures tk_getOpenFile and tk_getSaveFile pop up a dialog box for the user to select a file to open or save. The tk_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 tk_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...


#A procedure to make a toplevel window
proc makeTop { } {
	toplevel .top ;#Make the window
	#Put things in it
	label .top.lab -text "This is Toplevel Window" -font "ansi 12 bold"
	text .top.txt 
	.top.txt insert end "Widgets can be packed in this window."
	#An option to close the window.
	button .top.but -text "Close" -command { destroy .top }
	#Pack everything
	pack .top.lab .top.txt .top.but
}

label .lab -text "This is the root window." -font "ansi 12 bold"
button .but -text "Click to Create Toplevel" -command { makeTop }
pack .lab .but
Previous
Canvas, Message 
Next
Grid, Pack 
Subscribe to Feed