tk_messageBox
Guide: Dialogs
Shows a message with a set of buttons, waits for the user to click one, and gives its name.
tk_messageBox ?-option VALUE ...?
Description
Shows a modal dialog with an icon, the text of -message and -detail, and the buttons of -type. The command waits until the user clicks a button, and gives the name of that button, such as ok or yes. The Return key clicks the focused button. At the start, the focus is on the button of -default. The Escape key and the close button of the window give the answer cancel, or no, abort, or ok for a type with no cancel button.
On Windows, macOS, and iOS, the dialog is the message box of the platform. The other backends show a dialog that ztk draws with its widgets: a toplevel of the class Dialog with a grab. In the web build, the dialog is a frame of the class Dialog over the window of its parent. The texts of the buttons come from the messages of msgcat in the namespace ::tk, so an app can translate them.
While the dialog waits, the events of the app run, such as after timers. A test can answer the dialog with a timer that sends a key, as the examples do.
Options
| Option | Type | Default | Description |
|---|---|---|---|
-default | string | none | The name of the button that has the focus at the start. |
-detail | string | empty | A second text under the message, in a smaller font. |
-icon | string | info | The icon at the left of the message. |
-message | string | empty | The main text of the dialog. |
-parent | window | . | The window that the dialog belongs to. |
-title | string | empty | The title of the window of the dialog. |
-type | string | ok | The set of buttons. |
-default NAME
The name of the button that has the focus at the start.
The name must be a button of -type. The default is the first button of the type.
% after 10 {event generate [focus] <Key-Return>}
> tk_messageBox -type okcancel -default cancel -message "Delete the file?"
cancel% tk_messageBox -type ok -default cancel
bad -default value "cancel": must be abort, retry, ignore, ok, cancel, no, or yes
-errorcode NONE-detail TEXT
A second text under the message, in a smaller font.
The detail explains the message. An empty text shows no detail.
# The drawn dialog has the detail in a label of its own.
% after 10 {
> set ::shown [winfo exists .__tk__messagebox.top.dtl]
> event generate [focus] <Key-Return>
> }
> tk_messageBox -message "The file changed." -detail "Another program wrote it."
> set ::shown
1Any text is a detail.
-icon ICON
The icon at the left of the message.
The icon shows the kind of the message.
| Value | Description |
|---|---|
error | A fault that stopped an action. |
info | A fact for the user. |
question | A question for the user. |
warning | A risk that the user must know. |
% after 10 {event generate [focus] <Key-Return>}
> tk_messageBox -icon warning -message "The disk is almost full."
ok% tk_messageBox -icon stop
bad -icon value "stop": must be error, info, question, or warning
-errorcode NONE-message TEXT
The main text of the dialog.
The text breaks into lines at a width of 320 pixels in the drawn dialog. A newline in the text starts a new line.
% after 10 {event generate [focus] <Key-Return>}
> tk_messageBox -message "Done."
ok% tk_messageBox -message
value for "-message" missing
-errorcode NONE-parent WINDOW
The window that the dialog belongs to.
The dialog shows over the toplevel of WINDOW. The window must exist.
% toplevel .doc
> after 10 {event generate [focus] <Key-Return>}
> tk_messageBox -parent .doc -message "Saved."
ok% tk_messageBox -parent .nope
bad window path name ".nope"
-errorcode NONE-title TEXT
The title of the window of the dialog.
The drawn dialog shows the title in the bar of its toplevel, and in the web build at the top of its frame.
% after 10 {
> set ::title [wm title .__tk__messagebox]
> event generate [focus] <Key-Return>
> }
> tk_messageBox -title Editor -message "Saved."
> set ::title
EditorAny text is a title.
-type TYPE
The set of buttons.
Each type has its buttons in this order, from the left.
| Value | Description |
|---|---|
abortretryignore | The buttons abort, retry, and ignore. |
ok | The button ok. |
okcancel | The buttons ok and cancel. |
retrycancel | The buttons retry and cancel. |
yesno | The buttons yes and no. |
yesnocancel | The buttons yes, no, and cancel. |
% after 10 {event generate [focus] <Key-Escape>}
> tk_messageBox -type yesnocancel -message "Save the changes?"
cancel% tk_messageBox -type maybe
bad -type value "maybe": must be abortretryignore, ok, okcancel, retrycancel, yesno, or yesnocancel
-errorcode NONEReturns
The name of the button: abort, retry, ignore, ok, cancel, yes, or no.
Examples
# The timer presses Return on the focused button, the default `yes`.
% after 10 {event generate [focus] <Key-Return>}
> tk_messageBox -type yesno -message "Save the changes?"
yes
# Escape gives `no`, because the type has no `cancel` button.
% after 10 {event generate [focus] <Key-Escape>}
> tk_messageBox -type yesno -message "Save the changes?"
no
Errors
% tk_messageBox -text hi
bad option "-text": must be -default, -detail, -icon, -message, -parent, -title, or -type
-errorcode NONE
% tk_messageBox -message
value for "-message" missing
-errorcode NONE
Differences from Tcl
- On Linux and the other backends with no message box of the platform, ztk draws the dialog with its own widgets and theme. Tk 8.6.16 on X11 draws a dialog of its own.
- The errors have the
-errorcodeNONE. Tk 8.6.16 gives codes such asTK LOOKUP OPTIONandTK LOOKUP DLG_TYPE.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1