ttk::combobox
Guide: Themed widgets (ttk)
Makes a themed combobox: an entry field with a drop-down list of values.
ttk::combobox PATHNAME ?OPTION VALUE ...?
Description
Makes a themed combobox with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The combobox is a ttk::entry with an arrow at the right. A click on the arrow, or the Down key, opens a list of the -values. A pick from the list sets the text and sends the virtual event <<ComboboxSelected>> to the combobox.
The parent of PATHNAME must exist, and PATHNAME must not. The class of the combobox is TCombobox, the name of its style. The combobox has each option and each widget command of ttk::entry, and adds -values, -height, -postcommand, current, and set.
With -state readonly, the user can only pick a value from the list. ztk draws the list inside the window of the combobox. The list closes with the Escape key or a click outside it.
Widget commands
| Command | Description |
|---|---|
ttk::combobox bbox | Gives the box of a character of the text. |
ttk::combobox current | Gives the index of the text in -values, or sets the text to a value of the list. |
ttk::combobox delete | Deletes characters of the text. |
ttk::combobox get | Gives the text of the field. |
ttk::combobox icursor | Moves the insert cursor before a character. |
ttk::combobox index | Gives the numeric index of an index of the field. |
ttk::combobox insert | Inserts a text before a character. |
ttk::combobox selection | Gives, sets, or clears the selected text of the field. |
ttk::combobox set | Sets the text of the field. |
ttk::combobox validate | Runs the validation script now and gives its result. |
ttk::combobox xview | Gives or moves the visible part of a text longer than the field. |
ttk::combobox state | Gives the state flags of the widget, or turns flags on and off. |
ttk::combobox instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::combobox identify | Gives the name of the element of the widget at a point. |
ttk::combobox cget | Gives the value of an option of the widget. |
ttk::combobox configure | Gives or sets the options of the widget. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-background | color | empty | windowColor | The color of the field behind the text, in place of the color of the style. |
-class | class name | empty | The class of the widget for the option database and for the bindings, set only at creation. | |
-cursor | cursor name | empty | cursor | The shape of the pointer over the widget. |
-exportselection | boolean | 1 | exportSelection | Whether the selected text is also the selection of the display for other widgets and programs. |
-font | font | TkTextFont | font | The font of the text. |
-foreground | color | empty | textColor | The color of the text, in place of the color of the style. |
-height | integer | 10 | height | The most rows that the drop-down list shows at a time. |
-invalidcommand | script | empty | invalidCommand | The script that runs when the validation script gives false. |
-justify | justification | left | justify | The side of the field that the text lines up with, when the text is shorter than the field. |
-placeholder | string | empty | placeHolder | A hint text that shows while the field is empty. |
-placeholderforeground | color | #b3b3b3 | placeholderForeground | The color of the hint text of -placeholder. |
-postcommand | script | empty | postCommand | A script that runs each time before the list opens. |
-show | string | empty | show | A character that shows in place of each character of the text, such as for a password. |
-state | string | normal | state | Whether the user can change the text: normal, readonly, or disabled. |
-style | style name | empty | style | The style of the widget, such as TCombobox or a style of the script such as Accent.TCombobox. |
-takefocus | boolean, empty, or script | ttk::takefocus | takeFocus | Whether the Tab key can move the focus to the widget. |
-textvariable | variable name | empty | textVariable | A global variable that holds the text of the field. |
-validate | mode | none | validate | The events that run the validation script of -validatecommand. |
-validatecommand | script | empty | validateCommand | The validation script, which gives true for valid text. |
-values | list | empty | values | The list of the values of the drop-down list. |
-width | integer | 20 | width | The width of the field in average characters of the font. |
-xscrollcommand | script | empty | xScrollCommand | The command prefix that the field calls when its view changes, for a scrollbar. |
-background COLOR
The color of the field behind the text, in place of the color of the style.
A color is a name such as white, or #RGB, #RRGGBB, or #RRRRGGGGBBBB. An empty value uses the field color of the style.

-background {}
-background {#fde68a}-textvariable ::ref::name.% ttk::combobox .tcbbg -background lightyellow
> .tcbbg cget -background
lightyellow% ttk::combobox .tcbbg2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-class CLASS
The class of the widget for the option database and for the bindings, set only at creation.
The class name replaces the class of the style, such as TCombobox, in winfo class and in the bind tags of the widget. Give it in the call that makes the widget. A later configure of the option gives an error.
% ttk::combobox .tcbcl -class Fancy
> winfo class .tcbcl
Fancy
% ttk::combobox .tcbcl3
> winfo class .tcbcl3
TCombobox% ttk::combobox .tcbcl2
> .tcbcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-cursor CURSOR
The shape of the pointer over the widget.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. An empty value uses the cursor of the parent.
% ttk::combobox .tcbcu -cursor hand2
> .tcbcu cget -cursor
hand2ztk stores each value of the option with no check.
-exportselection BOOLEAN
Whether the selected text is also the selection of the display for other widgets and programs.
With a true value, a selection in the widget becomes the PRIMARY selection, so the selection get command gives it. With a false value, the selection stays inside the widget.
% ttk::combobox .tcbex -exportselection 0
> .tcbex cget -exportselection
0% ttk::combobox .tcbex2 -exportselection x
expected boolean value but got "x"
-errorcode TCL VALUE NUMBER-font FONT
The font of the text.
A font name, such as TkTextFont, or a font description, such as {Helvetica 12 bold}. A larger font makes a taller widget, and -width counts characters of this font.

-font TkDefaultFont
-font TkFixedFont
-font {Helvetica 16 bold}-textvariable ::ref::name.% ttk::combobox .tcbfo
> ttk::combobox .tcbfo2 -font {Helvetica 24}
> expr {[winfo reqheight .tcbfo2] > [winfo reqheight .tcbfo]}
1% ttk::combobox .tcbfo3 -font {Helvetica 12 weird}
unknown font style "weird"
-errorcode TK LOOKUP FONT_STYLE weird-foreground COLOR
The color of the text, in place of the color of the style.
The color of the text. An empty value uses the color of the style.

-foreground {}
-foreground {#b91c1c}
-foreground {#2563eb}-textvariable ::ref::name.% ttk::combobox .tcbfg -foreground #123456
> .tcbfg cget -foreground
#123456% ttk::combobox .tcbfg2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-height ROWS
The most rows that the drop-down list shows at a time.
A list with more values than this count scrolls.
% ttk::combobox .tcbhe -height 5
> .tcbhe cget -height
5ztk stores each value at configure, as Tk 8.6.16 does.
-invalidcommand SCRIPT
The script that runs when the validation script gives false.
The script runs at the global level after -validatecommand gives false. It has the same % substitutions as -validatecommand. A common use is bell.
% ttk::combobox .tcbic -validate key -validatecommand {string is digit %P} -invalidcommand {set ::tcbbad %S}
> .tcbic insert 0 x
> set ::tcbbad
xEach string is a valid script.
-justify SIDE
The side of the field that the text lines up with, when the text is shorter than the field.
left puts the text at the left edge, right at the right edge, and center in the middle.
Values: left, center, right.
% ttk::combobox .tcbju -justify right
> .tcbju cget -justify
right% ttk::combobox .tcbju2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle-placeholder TEXT
A hint text that shows while the field is empty.
The field shows the text in the color of -placeholderforeground while it has no text. The hint is not part of the value: get gives an empty string.

-placeholder {}
-placeholder {Your name}-textvariable ::ref::empty.% ttk::combobox .tcbph -placeholder Name
> list [.tcbph get] [.tcbph cget -placeholder]
{} NameEach string is a valid text.
-placeholderforeground COLOR
The color of the hint text of -placeholder.
The color of the text of -placeholder while the field is empty.

-placeholderforeground {#b91c1c}-textvariable ::ref::empty -placeholder Your name.% ttk::combobox .tcbpf -placeholderforeground gray50
> .tcbpf cget -placeholderforeground
gray50% ttk::combobox .tcbpf2 -placeholderforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-postcommand SCRIPT
A script that runs each time before the list opens.
The script runs at the global level before the list opens, so it can set -values to a new list. A combobox with the flag disabled does not open its list and runs no script.
% ttk::combobox .tcbpc -postcommand {.tcbpc configure -values [list a b c]}
> pack .tcbpc
> update
> focus -force .tcbpc
> event generate .tcbpc <Down>
> .tcbpc cget -values
a b cEach string is a valid script.
-show CHAR
A character that shows in place of each character of the text, such as for a password.
With a value, the field shows the first character of the value once for each character of the text. get gives the real text. An empty value shows the text.

-show {}
-show *-textvariable ::ref::name.% ttk::combobox .tcbsh -show *
> .tcbsh insert 0 secret
> .tcbsh get
secretEach string is a valid value.
-state STATE
Whether the user can change the text: normal, readonly, or disabled.
readonly sets the state flag readonly: the user can select and copy the text, and insert and delete make no change. disabled sets the flag disabled: the widget ignores the keyboard and the pointer and has the disabled look. normal clears the two flags. ztk stores each other value with no check.
| Value | Description |
|---|---|
normal | The text can change. |
readonly | The text is fixed, and the user can select it. |
disabled | The text is fixed, and the widget ignores the keyboard and the pointer. |

-state normal
-state disabled-textvariable ::ref::name.% ttk::combobox .tcbst -state readonly
> .tcbst insert 0 abc
> list [.tcbst get] [.tcbst state]
{} readonlyztk stores each value of the option with no check, as Tk 8.6.16 does.
-style STYLE
The style of the widget, such as TCombobox or a style of the script such as Accent.TCombobox.
An empty value uses the style TCombobox. A style name of the script ends with the name of the base style, such as Accent.TCombobox, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.
% ttk::style configure Accent.TCombobox -foreground white
> ttk::combobox .tcbsy -style Accent.TCombobox
> .tcbsy cget -style
Accent.TComboboxztk stores each value of the option with no check, as Tk 8.6.16 does at creation.
-takefocus VALUE
Whether the Tab key can move the focus to the widget.
1 puts the widget in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk. A script, such as the default ttk::takefocus, runs with the path of the widget appended, and its result is the answer. ttk::takefocus gives 1 when the widget is not disabled and is viewable.
% ttk::combobox .tcbtf -takefocus 0
> .tcbtf cget -takefocus
0ztk stores each value of the option with no check.
-textvariable VAR
A global variable that holds the text of the field.
The field shows the value of the global variable VAR. A change of the text sets the variable, and a change of the variable changes the text.
% set ::tcbname Ada
> ttk::combobox .tcbtv -textvariable ::tcbname
> .tcbtv insert end " Lovelace"
> set ::tcbname
Ada LovelaceEach name is a valid variable name.
-validate MODE
The events that run the validation script of -validatecommand.
The script of -validatecommand runs at each event of the mode. When the script gives false for a change of the text by a key, the change does not happen. An error of the script sets the mode to none.
| Value | Description |
|---|---|
none | No validation. |
key | A change of the text by a key, insert, or delete. |
focus | The widget gets or loses the focus. |
focusin | The widget gets the focus. |
focusout | The widget loses the focus. |
all | Each of the events above. |
% ttk::combobox .tcbva -validate key -validatecommand {string is digit %P}
> .tcbva insert 0 12
> .tcbva insert end a
> .tcbva get
12% ttk::combobox .tcbva2 -validate bogus
bad validate "bogus": must be all, key, focus, focusin, focusout, or none
-errorcode TCL LOOKUP INDEX validate bogus-validatecommand SCRIPT
The validation script, which gives true for valid text.
Before the script runs, each % code in it changes to a value: %d is 1 for an insert, 0 for a delete, and -1 for other events. %i is the index of the change, %P the text after the change, %s the text before it, and %S the text of the insert or the delete. %v is the mode of -validate, %V the event (key, focusin, focusout, or forced), and %W the path of the widget.
% ttk::combobox .tcbvc -validate key -validatecommand {set ::tcbargs [list %d %i %P %s %S %V]; expr 1}
> .tcbvc insert 0 ab
> set ::tcbargs
1 0 ab {} ab keyEach string is a valid script.
-values LIST
The list of the values of the drop-down list.
Each element of the list is one row of the list. current gives and sets the text by its index in this list.
% ttk::combobox .tcbvl -values {a {b c}}
> .tcbvl current 1
> .tcbvl get
b cztk stores each value at configure, as Tk 8.6.16 does.
-width CHARS
The width of the field in average characters of the font.
The field asks for room for this count of characters. A longer text scrolls inside the field.

-width 8
-width 20
-width 30-textvariable ::ref::name.% ttk::combobox .tcbwi -width 10
> ttk::combobox .tcbwi30 -width 30
> expr {[winfo reqwidth .tcbwi30] > [winfo reqwidth .tcbwi]}
1% ttk::combobox .tcbwi2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-xscrollcommand PREFIX
The command prefix that the field calls when its view changes, for a scrollbar.
When the visible part of the text changes, the field calls the prefix with two fractions appended: the start and the end of the visible part. Give it the set command of a horizontal scrollbar. The prefix also runs when the text changes.
% ttk::combobox .tcbxs -width 10 -xscrollcommand {lappend ::tcbview}
> pack .tcbxs
> .tcbxs insert 0 [string repeat abc 50]
> update
> lassign [lrange $::tcbview end-1 end] first last
> expr {$last - $first < 1}
1Each string is a valid prefix.
Bindings
| Event | Description |
|---|---|
<Key> | Inserts the character of the key at the insert cursor, in place of the selection. Tab, Return, Escape, and the keys with Control or Alt insert no text. |
<Key-BackSpace> | Deletes the selection, or the character before the insert cursor. |
<Key-Delete> | Deletes the selection, or the character after the insert cursor. |
<Key-Left> | Moves the insert cursor one character to the left and clears the selection. With Shift, the selection grows or shrinks to the new place. |
<Key-Right> | Moves the insert cursor one character to the right and clears the selection. With Shift, the selection grows or shrinks to the new place. |
<Control-Key-Left> | Moves the insert cursor to the start of the word before it. |
<Control-Key-Right> | Moves the insert cursor to the end of the word after it. |
<Key-Home> | Moves the insert cursor to the start of the text. With Shift, the selection grows to the start. Control-a does the same as Home. |
<Key-End> | Moves the insert cursor to the end of the text. With Shift, the selection grows to the end. Control-e does the same as End. |
<Button-1> | Places the insert cursor at the pointer, clears the selection, and gives the widget the focus. |
<B1-Motion> | Selects the text from the place of the press to the pointer, and scrolls the text when the pointer is past an end. |
<Shift-Button-1> | The selection grows or shrinks to the pointer. |
<Double-Button-1> | Selects the word under the pointer. |
<Triple-Button-1> | Selects all of the text. |
<Shift-MouseWheel> | Scrolls the text to the left or to the right. |
<FocusIn> | The insert cursor shows and blinks. |
<FocusOut> | The insert cursor goes. |
<Control-Key-c> | Sends <<Copy>>. |
<Control-Key-x> | Sends <<Cut>>. |
<Control-Key-v> | Sends <<Paste>>. |
<Control-Key-z> | Sends <<Undo>>. |
<Control-Key-y> | Sends <<Redo>>. Control-Shift-z does the same. |
<Control-Key-slash> | Sends <<SelectAll>>. |
<Control-Key-backslash> | Sends <<SelectNone>>. |
<Button-1> | A press on the arrow, or on a combobox with the state readonly, opens the list of -values. With the list open, a press on a row selects the row, and a press away from the list closes it. |
<ButtonRelease-1> | After a press that opened the list, a release on a row selects the row. |
<Motion> | With the list open, the row under the pointer is the active row. |
<MouseWheel> | With the list open, scrolls the list. |
<Key-Down> | Opens the list, or with the list open, makes the next row the active row. |
<Key-Up> | With the list open, makes the row before the active row. |
<Key-Return> | With the list open, selects the active row. |
<Key-Escape> | Closes the list with no change of the value. |
Virtual events
| Event | Description |
|---|---|
<<Copy>> | Puts the selected text on the clipboard. With -show, the clipboard gets no text. |
<<Cut>> | Puts the selected text on the clipboard and deletes it. |
<<Paste>> | Inserts the text of the clipboard at the insert cursor, in place of the selection. |
<<Clear>> | Deletes the selected text. |
<<Undo>> | Undoes the last edit. |
<<Redo>> | Makes the last edit that <<Undo>> undid again. |
<<SelectAll>> | Selects all of the text. event generate .c <<SelectAll>> sends it. |
<<SelectNone>> | Clears the selection. |
<<ComboboxSelected>> | The combobox sends it after a row of the list sets the value. The text is then all selected. |
States
| State | Description |
|---|---|
focus | The widget has the keyboard focus: the insert cursor shows, and the theme draws the focus ring. |
disabled | The widget ignores the keyboard and the pointer, and the theme draws it grayed. |
readonly | The text can be selected and copied, and no key changes it. |
invalid | A script turns it on with state invalid, as the result of a check of the value. |
hover | The pointer is over the widget. |

no state flag
state focus
state disabled-textvariable ::ref::name.Returns
PATHNAME.
Examples
% ttk::combobox .color -values {red green blue}
> .color current 1
> .color get
green
% ttk::combobox .size -values {S M L} -state readonly
> .size set L
> list [.size get] [.size current]
L 2
% winfo class [ttk::combobox .city]
TCombobox
Errors
% ttk::combobox .x -foo a
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% ttk::combobox .twice
> ttk::combobox .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::combobox .no.c
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::combobox
wrong # args: should be "ttk::combobox pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
-placeholderand-placeholderforegroundare additions of ztk. Tk 8.6.16 does not have them.selectionhasfrom,to, andadjust, as the classic entry has. Theselectionof Tk 8.6.16 has onlyclear,present, andrange.- A false result of
-validatecommanddoes not turn on the state flaginvalid. Tk 8.6.16 turns it on. - An error of
-validatecommandgoes to the error output, and theinsertordeletegives no error. Tk 8.6.16 gives the error of the script. In both, the error sets-validatetonone. - An
insertscrolls the field to show the insert cursor. Tk 8.6.16 does not scroll the field. -cursoraccepts each value with no check. Tk 8.6.16 gives an error for a cursor name that does not exist.- The errors of the widget commands, such as a bad index or a wrong count of arguments, have the error code
NONE. Tk 8.6.16 gives a code such asTTK ENTRY INDEXorTCL WRONGARGS. The error of-widthhas the codeTCL VALUE NUMBER, where Tk 8.6.16 givesTCL VALUE INTEGER. - ztk draws the list in the window of the combobox. No toplevel
$cb.popdownand no listbox exist, and the procedures of Tk such asttk::combobox::Postdo not exist. A combobox with no values does not open its list. - The requested width differs from Tk 8.6.16 by the width of the arrow and the padding of the theme.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1