beetlebug▾
/

entry

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 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

CommandDescription
entry bboxGives the box of the character at INDEX.
entry deleteDeletes the characters from FIRST up to LAST.
entry getGives the text of the entry.
entry icursorMoves the insertion cursor to INDEX.
entry indexGives the number of the place that INDEX names.
entry insertInserts TEXT before the character at INDEX.
entry scanScrolls the entry by a drag of the mouse.
entry selectionReads or changes the selected text of the entry.
entry validateRuns the script of -validatecommand now.
entry xviewReads or changes the horizontal view of a text longer than the entry.
entry cgetGives the value of an option of the entry.
entry configureGives or sets the options of the entry.

Options

OptionTypeDefaultDatabase nameDescription
-backgroundcolor#ffffffbackgroundThe background color of the entry.
-borderwidthscreen distance1borderWidthThe width of the border of the entry.
-cursorcursor namextermcursorThe shape of the pointer over the entry.
-disabledbackgroundcolor#d9d9d9disabledBackgroundThe background color while the state is disabled.
-disabledforegroundcolor#a3a3a3disabledForegroundThe text color while the state is disabled.
-exportselectionboolean1exportSelectionWhether the selection of the entry is also the selection of the application.
-fontfontTkTextFontfontThe font of the text.
-foregroundcolor#000000foregroundThe color of the text.
-highlightbackgroundcolor#d9d9d9highlightBackgroundThe color of the focus ring while the entry has no focus, in the look of Tk on X11.
-highlightcolorcolor#000000highlightColorThe color of the focus ring while the entry has the focus, in the look of Tk on X11.
-highlightthicknessscreen distance1highlightThicknessThe width of the focus ring, in the look of Tk on X11.
-insertbackgroundcolor#000000insertBackgroundThe color of the insertion cursor.
-insertborderwidthscreen distance0insertBorderWidthThe width of the border of the insertion cursor.
-insertofftimeinteger300insertOffTimeThe time in milliseconds that the insertion cursor is hidden in each blink.
-insertontimeinteger600insertOnTimeThe time in milliseconds that the insertion cursor shows in each blink.
-insertwidthscreen distance2insertWidthThe width of the insertion cursor.
-invalidcommandscriptemptyinvalidCommandA script that runs when -validatecommand gives false.
-justifyjustificationleftjustifyThe place of the text in an entry wider than the text.
-readonlybackgroundcolor#d9d9d9readonlyBackgroundThe background color while the state is readonly.
-reliefreliefsunkenreliefThe 3-D look of the border.
-selectbackgroundcolor#c3c3c3selectBackgroundThe background color of the selected text.
-selectborderwidthscreen distance0selectBorderWidthThe width of the border around the selection.
-selectforegroundcolor#000000selectForegroundThe text color of the selected text.
-showstringemptyshowA character to show in place of each character of the text, for a password.
-statestatenormalstateWhether the user and a script can edit the text.
-takefocusstringemptytakeFocusWhether the Tab key can move the focus to the entry.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the entry.
-validatevalidatenonevalidateWhen the entry runs -validatecommand.
-validatecommandscriptemptyvalidateCommandA script that checks a change of the text.
-widthinteger20widthThe width of the entry in average characters of the font.
-xscrollcommandscriptemptyxScrollCommandA command prefix that the entry calls when its horizontal view changes.
-placeholderstringemptyplaceHolderA hint text that the entry shows while it is empty.
-placeholderforegroundcolor#b3b3b3placeholderForegroundThe 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.

entry with -textvariable ::ref::name and -background {#fde68a}-background {#fde68a}entry with -textvariable ::ref::name and -background {#bfdbfe}-background {#bfdbfe}
Each with -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.

entry with -textvariable ::ref::name -relief solid and -borderwidth 0-borderwidth 0entry with -textvariable ::ref::name -relief solid and -borderwidth 1-borderwidth 1entry with -textvariable ::ref::name -relief solid and -borderwidth 4-borderwidth 4
Each with -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.

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.

entry with -textvariable ::ref::name -state disabled and -disabledbackground {#fde68a}-disabledbackground {#fde68a}
Each with -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.

entry with -textvariable ::ref::name -state disabled and -disabledforeground {#b91c1c}-disabledforeground {#b91c1c}
Each with -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.

entry with -textvariable ::ref::name and -font TkDefaultFont-font TkDefaultFontentry with -textvariable ::ref::name and -font TkFixedFont-font TkFixedFontentry with -textvariable ::ref::name and -font {Helvetica 16 bold}-font {Helvetica 16 bold}
Each with -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.

entry with -textvariable ::ref::name and -foreground {#b91c1c}-foreground {#b91c1c}entry with -textvariable ::ref::name and -foreground {#2563eb}-foreground {#2563eb}
Each with -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
x

ztk 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.

entry with -textvariable ::ref::name -state readonly and -readonlybackground {#fde68a}-readonlybackground {#fde68a}
Each with -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.

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.

entry with -textvariable ::ref::name and -show {}-show {}entry with -textvariable ::ref::name and -show *-show *
Each with -textvariable ::ref::name.
% entry .show -show *
> .show insert 0 secret
> .show get
secret

Each 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.

entry with -textvariable ::ref::name and -state normal-state normalentry with -textvariable ::ref::name and -state disabled-state disabled
Each with -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
1

ztk 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
hi

Each 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 get

ztk 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.

entry with -textvariable ::ref::name and -width 8-width 8entry with -textvariable ::ref::name and -width 20-width 20entry with -textvariable ::ref::name and -width 30-width 30
Each with -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 1

ztk 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.

entry with -textvariable ::ref::empty and -placeholder {}-placeholder {}entry with -textvariable ::ref::empty and -placeholder {Your name}-placeholder {Your name}
Each with -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.

entry with -textvariable ::ref::empty -placeholder Your name and -placeholderforeground {#b91c1c}-placeholderforeground {#b91c1c}
Each with -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 nocolor

Bindings

EventDescription
<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

EventDescription
<<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

Facts

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

See also