beetlebug▾
/

tk_messageBox

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

OptionTypeDefaultDescription
-defaultstringnoneThe name of the button that has the focus at the start.
-detailstringemptyA second text under the message, in a smaller font.
-iconstringinfoThe icon at the left of the message.
-messagestringemptyThe main text of the dialog.
-parentwindow.The window that the dialog belongs to.
-titlestringemptyThe title of the window of the dialog.
-typestringokThe 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
1

Any text is a detail.

-icon ICON

The icon at the left of the message.

The icon shows the kind of the message.

ValueDescription
errorA fault that stopped an action.
infoA fact for the user.
questionA question for the user.
warningA 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
Editor

Any text is a title.

-type TYPE

The set of buttons.

Each type has its buttons in this order, from the left.

ValueDescription
abortretryignoreThe buttons abort, retry, and ignore.
okThe button ok.
okcancelThe buttons ok and cancel.
retrycancelThe buttons retry and cancel.
yesnoThe buttons yes and no.
yesnocancelThe 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 NONE

Returns

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

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1