beetlebug▾
/

tk_chooseColor

Shows a dialog to choose a color, and gives the color in the form #RRGGBB.

tk_chooseColor ?-option VALUE ...?

Description

Shows a modal dialog of colors, waits until the user chooses a color or cancels, and gives the color as # and six hex digits in lower case. A cancel gives an empty string.

On Windows and macOS, the dialog is the color dialog of the platform. The other backends show a dialog that ztk draws with its widgets: 24 swatches of color, a preview, a scale for each of red, green, and blue, and a field for the hex value. The Return key gives the color of the dialog, and the Escape key cancels. In the web build, the dialog is a frame over the window of its parent.

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
-initialcolorcoloremptyThe color that the dialog shows at the start.
-parentwindow.The window that the dialog belongs to.
-titlestringnoneThe title of the window of the dialog.

-initialcolor COLOR

The color that the dialog shows at the start.

Any color of Tk, such as red or #1e90ff. With no initial color, the dialog starts with the gray #808080.

% after 10 {event generate [focus] <Key-Return>}
> tk_chooseColor -initialcolor #1E90FF
#1e90ff

% after 10 {event generate [focus] <Key-Return>}
> tk_chooseColor
#808080
% tk_chooseColor -initialcolor nocolor
unknown color name "nocolor"
-errorcode NONE

-parent WINDOW

The window that the dialog belongs to.

The dialog shows over the toplevel of WINDOW. The window must exist.

% toplevel .paint
> after 10 {event generate [focus] <Key-Return>}
> tk_chooseColor -parent .paint -initialcolor red
#ff0000
% tk_chooseColor -parent .nope
bad window path name ".nope"
-errorcode NONE

-title TEXT

The title of the window of the dialog.

The default is Choose a color, in the language of msgcat for the dialog that ztk draws.

% after 10 {
>     set ::title [wm title .__tk__color]
>     event generate [focus] <Key-Return>
> }
> tk_chooseColor -title "Text color"
> set ::title
Text color
% tk_chooseColor -title
value for "-title" missing
-errorcode NONE

Returns

The color as #rrggbb, or an empty string after a cancel.

Examples

# The timer presses Return, so the dialog gives its first color.
% after 10 {event generate [focus] <Key-Return>}
> tk_chooseColor -initialcolor orange
#ffa500

# Escape cancels the dialog.
% after 10 {event generate [focus] <Key-Escape>}
> tk_chooseColor -initialcolor blue

Errors

% tk_chooseColor -color red
bad option "-color": must be -initialcolor, -parent, or -title
-errorcode NONE

% tk_chooseColor -title
value for "-title" missing
-errorcode NONE

Differences from Tcl

Facts

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

See also