entry
Guide: Entry and spinbox
Makes an entry: a widget that edits one line of text.
entry PATHNAME ?OPTION VALUE ...?
Description
Makes an entry widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The user types into the entry, and get gives its text. With -textvariable, a global variable and the text stay equal.
The parent of PATHNAME must exist, and PATHNAME must not. An entry is an Entry in CSS and for the option database, and ztk::class gives it a style class for a stylesheet.
Many widget commands take an index of a character. An index is one of these forms:
- A number from 0. A number past the end is the end, and a negative number is 0.
end: the place after the last character.insert: the place of the insertion cursor.anchor: the fixed end of the selection, fromselection fromorselection adjust.sel.firstandsel.last: the first selected character and the place after the last one. Each gives an error when the entry has no selection.@X: the character at the pixel X from the left side of the entry.
A change of the text from a script runs the validation of -validate, as a change of the user does. ztk adds -placeholder, a hint text for an empty entry, and an undo of the edits of the user.
Widget commands
| Command | Description |
|---|---|
entry bbox | Gives the box of the character at INDEX. |
entry delete | Deletes the characters from FIRST up to LAST. |
entry get | Gives the text of the entry. |
entry icursor | Moves the insertion cursor to INDEX. |
entry index | Gives the number of the place that INDEX names. |
entry insert | Inserts TEXT before the character at INDEX. |
entry scan | Scrolls the entry by a drag of the mouse. |
entry selection | Reads or changes the selected text of the entry. |
entry validate | Runs the script of -validatecommand now. |
entry xview | Reads or changes the horizontal view of a text longer than the entry. |
entry cget | Gives the value of an option of the entry. |
entry configure | Gives or sets the options of the entry. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-background | color | #ffffff | background | The background color of the entry. |
-borderwidth | screen distance | 1 | borderWidth | The width of the border of the entry. |
-cursor | cursor name | xterm | cursor | The shape of the pointer over the entry. |
-disabledbackground | color | #d9d9d9 | disabledBackground | The background color while the state is disabled. |
-disabledforeground | color | #a3a3a3 | disabledForeground | The text color while the state is disabled. |
-exportselection | boolean | 1 | exportSelection | Whether the selection of the entry is also the selection of the application. |
-font | font | TkTextFont | font | The font of the text. |
-foreground | color | #000000 | foreground | The color of the text. |
-highlightbackground | color | #d9d9d9 | highlightBackground | The color of the focus ring while the entry has no focus, in the look of Tk on X11. |
-highlightcolor | color | #000000 | highlightColor | The color of the focus ring while the entry has the focus, in the look of Tk on X11. |
-highlightthickness | screen distance | 1 | highlightThickness | The width of the focus ring, in the look of Tk on X11. |
-insertbackground | color | #000000 | insertBackground | The color of the insertion cursor. |
-insertborderwidth | screen distance | 0 | insertBorderWidth | The width of the border of the insertion cursor. |
-insertofftime | integer | 300 | insertOffTime | The time in milliseconds that the insertion cursor is hidden in each blink. |
-insertontime | integer | 600 | insertOnTime | The time in milliseconds that the insertion cursor shows in each blink. |
-insertwidth | screen distance | 2 | insertWidth | The width of the insertion cursor. |
-invalidcommand | script | empty | invalidCommand | A script that runs when -validatecommand gives false. |
-justify | justification | left | justify | The place of the text in an entry wider than the text. |
-readonlybackground | color | #d9d9d9 | readonlyBackground | The background color while the state is readonly. |
-relief | relief | sunken | relief | The 3-D look of the border. |
-selectbackground | color | #c3c3c3 | selectBackground | The background color of the selected text. |
-selectborderwidth | screen distance | 0 | selectBorderWidth | The width of the border around the selection. |
-selectforeground | color | #000000 | selectForeground | The text color of the selected text. |
-show | string | empty | show | A character to show in place of each character of the text, for a password. |
-state | state | normal | state | Whether the user and a script can edit the text. |
-takefocus | string | empty | takeFocus | Whether the Tab key can move the focus to the entry. |
-textvariable | variable name | empty | textVariable | A global variable whose value is the text of the entry. |
-validate | validate | none | validate | When the entry runs -validatecommand. |
-validatecommand | script | empty | validateCommand | A script that checks a change of the text. |
-width | integer | 20 | width | The width of the entry in average characters of the font. |
-xscrollcommand | script | empty | xScrollCommand | A command prefix that the entry calls when its horizontal view changes. |
-placeholder | string | empty | placeHolder | A hint text that the entry shows while it is empty. |
-placeholderforeground | color | #b3b3b3 | placeholderForeground | The color of the hint text of -placeholder. |
-background COLOR
The background color of the entry.
The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-background {#fde68a}
-background {#bfdbfe}-textvariable ::ref::name.% entry .background -background {#336699}
> .background cget -background
#336699% entry .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-borderwidth DISTANCE
The width of the border of the entry.
The width of the border around the entry. -bd is the same option. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-borderwidth 0
-borderwidth 1
-borderwidth 4-textvariable ::ref::name -relief solid.% entry .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4% entry .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-cursor CURSOR
The shape of the pointer over the entry.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. A value that starts with @ names a file. An empty value uses the cursor of the parent.
% entry .cursor -cursor hand2
> .cursor cget -cursor
hand2% entry .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR-disabledbackground COLOR
The background color while the state is disabled.
The color behind the text while -state is disabled. With no value from the script, the theme gives the color.

-disabledbackground {#fde68a}-textvariable ::ref::name -state disabled.% entry .disabledbackground -disabledbackground {#336699}
> .disabledbackground cget -disabledbackground
#336699% entry .disabledbackground2 -disabledbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-disabledforeground COLOR
The text color while the state is disabled.
The color of the text while -state is disabled.

-disabledforeground {#b91c1c}-textvariable ::ref::name -state disabled.% entry .disabledforeground -disabledforeground {#336699}
> .disabledforeground cget -disabledforeground
#336699% entry .disabledforeground2 -disabledforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-exportselection BOOLEAN
Whether the selection of the entry is also the selection of the application.
In Tk, a true value makes a selection in the entry the PRIMARY selection, and selection get gives it. ztk stores the value with no effect: the selection of the entry is not the PRIMARY selection.
% entry .exportselection -exportselection 0
> .exportselection cget -exportselection
0% entry .exportselection2 -exportselection maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-font FONT
The font of the text.
A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold}. A family that does not exist falls back to a font of the system.

-font TkDefaultFont
-font TkFixedFont
-font {Helvetica 16 bold}-textvariable ::ref::name.% entry .font -font {Helvetica 12 bold}
> .font cget -font
Helvetica 12 bold% entry .font2 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER-foreground COLOR
The color of the text.
The color of the text. -fg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-foreground {#b91c1c}
-foreground {#2563eb}-textvariable ::ref::name.% entry .foreground -foreground {#336699}
> .foreground cget -foreground
#336699% entry .foreground2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightbackground COLOR
The color of the focus ring while the entry has no focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the entry does not have the focus. ztk stores the value with no effect.
% entry .highlightbackground -highlightbackground {#336699}
> .highlightbackground cget -highlightbackground
#336699% entry .highlightbackground2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightcolor COLOR
The color of the focus ring while the entry has the focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the entry has the focus. ztk stores the value with no effect.
% entry .highlightcolor -highlightcolor {#336699}
> .highlightcolor cget -highlightcolor
#336699% entry .highlightcolor2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightthickness DISTANCE
The width of the focus ring, in the look of Tk on X11.
The width of the ring around the entry that shows the focus. The modern theme draws the focus with the stylesheet.
% entry .highlightthickness -highlightthickness 4
> .highlightthickness cget -highlightthickness
4% entry .highlightthickness2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS-insertbackground COLOR
The color of the insertion cursor.
The color of the bar that marks the place of the next typed character.
% entry .insertbackground -insertbackground {#336699}
> .insertbackground cget -insertbackground
#336699% entry .insertbackground2 -insertbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-insertborderwidth DISTANCE
The width of the border of the insertion cursor.
In Tk, the insertion cursor can have a 3-D border of this width. ztk stores the value with no effect, and cget gives the value of the script.
% entry .insertborderwidth -insertborderwidth 4
> .insertborderwidth cget -insertborderwidth
4% entry .insertborderwidth2 -insertborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-insertofftime N
The time in milliseconds that the insertion cursor is hidden in each blink.
0 stops the blink, and the cursor stays on.
% entry .insertofftime -insertofftime 5
> .insertofftime cget -insertofftime
5% entry .insertofftime2 -insertofftime x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-insertontime N
The time in milliseconds that the insertion cursor shows in each blink.
The cursor blinks only while the entry has the focus and -insertofftime is more than 0.
% entry .insertontime -insertontime 5
> .insertontime cget -insertontime
5% entry .insertontime2 -insertontime x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-insertwidth DISTANCE
The width of the insertion cursor.
A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.
% entry .insertwidth -insertwidth 4
> .insertwidth cget -insertwidth
4% entry .insertwidth2 -insertwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-invalidcommand SCRIPT
A script that runs when -validatecommand gives false.
The script runs after the validation rejects a change. It has the same % substitutions as -validatecommand. -invcmd is the same option.
% entry .invalidcommand -validate key -vcmd {string is digit %P} -invcmd {set ::bad %S}
> .invalidcommand insert 0 x
> set bad
xztk stores each script with no check.
-justify SIDE
The place of the text in an entry wider than the text.
The place of the text in an entry wider than the text: at the left side, in the middle, or at the right side.
Values: left, right, center.
% entry .justify -justify right
> .justify cget -justify
right% entry .justify2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle-readonlybackground COLOR
The background color while the state is readonly.
The color behind the text while -state is readonly. With no value from the script, the theme gives the color.

-readonlybackground {#fde68a}-textvariable ::ref::name -state readonly.% entry .readonlybackground -readonlybackground {#336699}
> .readonlybackground cget -readonlybackground
#336699% entry .readonlybackground2 -readonlybackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-relief RELIEF
The 3-D look of the border.
Each value side by side: the contact sheet of the reliefs
The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.
Values: flat, groove, raised, ridge, solid, sunken.
% entry .relief -relief groove
> .relief cget -relief
groove% entry .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-selectbackground COLOR
The background color of the selected text.
The color behind the characters of the selection. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.
% entry .selectbackground -selectbackground {#336699}
> .selectbackground cget -selectbackground
#336699% entry .selectbackground2 -selectbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-selectborderwidth DISTANCE
The width of the border around the selection.
In Tk, the selection can have a 3-D border of this width. ztk stores the value with no effect.
% entry .selectborderwidth -selectborderwidth 4
> .selectborderwidth cget -selectborderwidth
4% entry .selectborderwidth2 -selectborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-selectforeground COLOR
The text color of the selected text.
The color of the characters of the selection.
% entry .selectforeground -selectforeground {#336699}
> .selectforeground cget -selectforeground
#336699% entry .selectforeground2 -selectforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-show CHAR
A character to show in place of each character of the text, for a password.
With a value, the entry shows its first character once for each character of the text. get still gives the real text. An empty value shows the text.

-show {}
-show *-textvariable ::ref::name.% entry .show -show *
> .show insert 0 secret
> .show get
secretEach string is a valid value.
-state STATE
Whether the user and a script can edit the text.
normal allows edits. readonly shows the text, allows a selection, and blocks each edit, also an edit of insert and delete. disabled blocks the edits and the selection, and draws the text with -disabledforeground.
Values: disabled, normal, readonly.

-state normal
-state disabled-textvariable ::ref::name.# A readonly entry keeps its text.
% entry .state -state readonly
> .state insert 0 abc
> .state get% entry .state2 -state busy
bad state "busy": must be disabled, normal, or readonly
-errorcode TCL LOOKUP INDEX state busy-takefocus TEXT
Whether the Tab key can move the focus to the entry.
1 puts the entry in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.
% entry .takefocus -takefocus 1
> .takefocus cget -takefocus
1ztk stores each value of the option with no check.
-textvariable VAR
A global variable whose value is the text of the entry.
The entry shows the value of the global variable VAR, and changes its text when the variable changes. A change of the text sets the variable.
% set user ada
> entry .textvariable -textvariable user
> .textvariable get
ada
% entry .textvariable2 -textvariable note
> .textvariable2 insert 0 hi
> set note
hiEach name is a valid variable name.
-validate WORD
When the entry runs -validatecommand.
none runs no validation. key runs it at each change by the keyboard, focus, focusin, and focusout at a change of the focus, and all at each of these. An error in the script, or a script that changes the text, sets the mode to none.
Values: all, key, focus, focusin, focusout, none.
# The script rejects the x.
% entry .validate -validate key -vcmd {string is digit %P}
> .validate insert 0 42
> .validate insert end x
> .validate get
42% entry .validate2 -validate bogus
bad validate "bogus": must be all, key, focus, focusin, focusout, or none
-errorcode TCL LOOKUP INDEX validate bogus-validatecommand SCRIPT
A script that checks a change of the text.
The script gives a boolean. A false result rejects the change, and the entry keeps its old text. Before the run, %d, %i, %P, %s, %S, %v, %V, and %W in the script change to facts of the change. -vcmd is the same option.
# The text of four characters is too long.
% entry .validatecommand -validate key -validatecommand {expr {[string length %P] <= 3}}
> .validatecommand insert 0 abcd
> .validatecommand getztk stores each script with no check. An error of the script sets -validate to none.
-width CHARS
The width of the entry in average characters of the font.
The entry asks for room for this count of characters. 0 or less makes the entry as wide as its text.

-width 8
-width 20
-width 30-textvariable ::ref::name.% entry .width -width 5
> .width cget -width
5% entry .width2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-xscrollcommand SCRIPT
A command prefix that the entry calls when its horizontal view changes.
The entry calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% entry .xscrollcommand -xscrollcommand {set ::done 1}
> .xscrollcommand cget -xscrollcommand
set ::done 1ztk stores each script with no check. An error of the script shows at the time of the call.
-placeholder TEXT
A hint text that the entry shows while it is empty.
The entry shows the text in the color of -placeholderforeground while it has no text. The hint is not part of the value. This option is an addition of ztk.

-placeholder {}
-placeholder {Your name}-textvariable ::ref::empty.# The hint is not part of the text.
% entry .placeholder -placeholder Search
> list [.placeholder cget -placeholder] [.placeholder get]
Search {}Each string is a valid hint.
-placeholderforeground COLOR
The color of the hint text of -placeholder.
An empty value uses a gray of the theme. This option is an addition of ztk.

-placeholderforeground {#b91c1c}-textvariable ::ref::empty -placeholder Your name.% entry .placeholderforeground -placeholderforeground {#336699}
> .placeholderforeground cget -placeholderforeground
#336699% entry .placeholderforeground2 -placeholderforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolorBindings
| 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>>. |
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 .e <<SelectAll>> sends it. |
<<SelectNone>> | Clears the selection. |
Returns
PATHNAME.
Examples
% entry .name
> .name insert 0 "Main Street"
> .name get
Main Street
# A change of the text changes the variable.
% set city Paris
> entry .city -textvariable city
> .city insert end ", France"
> set city
Paris, France
% winfo class [entry .go]
Entry
Errors
% entry .x -foo b
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% entry .twice
> entry .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% entry .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% entry
wrong # args: should be "entry pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
- An error of a widget command, such as a bad index or a wrong count of arguments, has the
-errorcodeNONE. Tk 8.6.16 gives a code such asTK ENTRY BAD_INDEXorTCL WRONGARGS. -insertborderwidthis stored with no effect, andcgetgives the value of the script. Tk 8.6.16 limits the value to half of-insertwidth.-exportselection,-highlightbackground,-highlightcolor, and-selectborderwidthare stored with no effect.- The selection of an entry is not the
PRIMARYselection, soselection getgives an error. In Tk 8.6.16, with-exportselection 1,selection getgives the selected text. -placeholderand-placeholderforegroundare additions of ztk. Tk 8.6.16 has no such options.- The size in a
-fontdescription can have a fraction, such as{Helvetica 12.5}. Tk 8.6.16 needs an integer. -highlightthicknessand-highlightbackgroundhave no visible effect yet, and the reliefs other thanflatdraw the same border (ztk#763).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1