entry(n)
๐ธ entry(n) โ the Tk entry widget, explained by your favorite Unix bestie ๐ธ
okay so listen. this is the manual page for the entry command from Tk Built-In Commands, and it's literally the blueprint for that one-line text box you type into. think password fields, search bars, username inputs โ all of it. we're gonna go through it together, no gatekeeping, periodt. ๐
โจ NAME โจ
entry โ create and manipulate 'entry' one-line text entry widgets ๐
๐ฆ SYNOPSIS ๐ฆ
entry pathName ?options?
that's it. that's the whole vibe. pathName is where you want it, and then all the optional stuff. slay.
๐ STANDARD OPTIONS ๐
bestie these are the standard ones โ the whole aesthetic layer. and yes, they're listed exactly like this in the original, so here you go:
-background-borderwidth-cursor-exportselection-font-foreground-highlightbackground-highlightcolor-highlightthickness-insertbackground-insertborderwidth-insertofftime-insertontime-insertwidth-justify-placeholder-placeholderforeground-relief-selectbackground-selectborderwidth-selectforeground-takefocus-textvariable-xscrollcommand
๐ see the options manual entry for the full tea on the standard options. iykyk.
๐ WIDGET-SPECIFIC OPTIONS ๐
these are the ones that make entry entry and not just some random widget. the way each one has a command-line name, a database name, and a database class โ it's giving organized. ๐ญ
-disabledbackgroundโ database namedisabledBackground, database classDisabledBackground
sets the background color used when the entry is disabled. if this option is the empty string, the normal background color gets used instead. simple as that. ๐
-disabledforegroundโ database namedisabledForeground, database classDisabledForeground
same energy but for the foreground color when the entry is disabled. empty string? normal foreground color, no cap. โจ
-invalidcommandor-invcmdโ database nameinvalidCommand, database classInvalidCommand
this one is a whole script that gets eval'd when -validatecommand returns 0. setting it to {} disables the feature (and that's the default, fyi). the manual literally says "the best use of this option is to set it to bell" โ iconic recommendation ngl. see VALIDATION below for the full story. ๐
-readonlybackgroundโ database namereadonlyBackground, database classReadonlyBackground
background color when the entry is readonly. if it's the empty string, the normal background color is used. we love a clean fallback. ๐
-showโ database nameshow, database classShow
ooh this one is sneaky. if you set it, the true contents of the entry are not displayed in the window. instead, each character in the entry's value gets displayed as the first character in the value of this option โ like *. this is lowkey perfect for password fields, obviously. โ ๏ธ but listen: if characters in the entry are selected and copied elsewhere, the info copied will be what is displayed, not the true contents. so don't be surprised, bestie. ๐
-stateโ database namestate, database classState
specifies one of three states for the entry: normal, disabled, or readonly. let's break it down:
readonly? the value may not be changed using widget commands, and no insertion cursor will be displayed even if the input focus is in the widget โ but the contents can still be selected. ๐disabled? the value may not be changed, no insertion cursor shows up, the contents aren't selectable, and the entry may display in a different color depending on the-disabledforegroundand-disabledbackgroundoptions. very much "you can look but don't touch" energy. ๐
-validateโ database namevalidate, database classValidate
specifies the mode validation runs in: none, focus, focusin, focusout, key, or all. defaults to none. and like โ when you actually want validation you have to explicitly state which mode you want. no mind reading here. see VALIDATION below for more. ๐ฅ
-validatecommandor-vcmdโ database namevalidateCommand, database classValidateCommand
a script to eval when you want to validate the input into the entry widget. setting it to {} disables this feature (default, again). this command must return a valid Tcl boolean value. here's the deal:
- returns
0(or the valid Tcl boolean equivalent)? you're rejecting the new edition โ it will not occur, and-invalidcommandgets evaluated if it's set. - returns
1? the new edition happens. periodt.
see VALIDATION below for the deep dive. โ
-widthโ database namewidth, database classWidth
an integer value for the desired width of the entry window, measured in average-size characters of the widget's font. if the value is negative or zero, the widget picks a size just large enough to hold its current text. tiny but mighty. ๐ง
๐ DESCRIPTION ๐
okay so here's the main character energy of this whole page.
the entry command creates a new window (that's your pathName argument) and makes it into an entry widget. additional options โ the ones we just went through โ can be specified on the command line or in the option database to configure the entry's colors, font, relief, all that. the entry command returns its pathName argument. ๐ซถ
โ ๏ธ important: at the time this command is invoked, there must not already exist a window named pathName, but pathName's parent must exist. no orphan windows, bestie.
so what even is an entry? it's a widget that displays a one-line text string and lets that string be edited using widget commands (described below), which are typically bound to keystrokes and mouse actions. when first created, an entry's string is empty. a portion of the entry may be selected. ๐
if an entry is exporting its selection (peep the -exportselection option), it observes the standard X11 protocols for handling the selection โ entry selections are available as type STRING. entries also observe the standard Tk rules for dealing with the input focus. when an entry has the input focus, it displays an insertion cursor to show where new characters will be inserted. very demure, very mindful. โจ
entries are also capable of displaying strings that are too long to fit entirely within the widget's window. in that case only a portion of the string displays, and commands described below can change the view in the window. entries use the standard -xscrollcommand mechanism for interacting with scrollbars (see the description of the -xscrollcommand option for the details). they also support scanning, described below. ๐
๐ VALIDATION ๐
okay this is the big one. validation works by setting the -validatecommand option to a script (validateCommand) which gets evaluated according to the -validate option, like this:
noneโ the default. this means no validation will occur. ๐คfocusโvalidateCommandwill be called when the entry receives or loses focus.focusinโvalidateCommandwill be called when the entry receives focus.focusoutโvalidateCommandwill be called when the entry loses focus.keyโvalidateCommandwill be called when the entry is edited. โจ๏ธallโvalidateCommandwill be called for all above conditions. ๐
it is possible to perform percent substitutions on the value of the -validatecommand and -invalidcommand options, just like you would in a bind script. the following substitutions are recognized:
%dโ type of action:1for insert,0for delete, or-1for focus, forced or textvariable validation.%iโ index of char string to be inserted/deleted, if any, otherwise-1.%Pโ the value of the entry if the edit is allowed. if you are configuring the entry widget to have a newtextvariable, this will be the value of thattextvariable.%sโ the current value of entry prior to editing.%Sโ the text string being inserted/deleted, if any,{}otherwise.%vโ the type of validation currently set.%Vโ the type of validation that triggered the callback (key,focusin,focusout,forced).%Wโ the name of the entry widget.
๐ the warning you need to hear: in general, the-textvariableand-validatecommandoptions can be dangerous to mix. the problems have been overcome so that using-validatecommandwill not interfere with the traditional behavior of the entry widget. using the-textvariablefor read-only purposes will never cause problems. the danger comes when you try to set the-textvariableto something that the-validatecommandwould not accept โ that causes-validateto becomenone(and the-invalidcommandwill not be triggered). the same happens when an error occurs evaluating the-validatecommand.
primarily, an error occurs when the -validatecommand or -invalidcommand encounters an error in its script while evaluating, or when -validatecommand does not return a valid Tcl boolean value. the -validate option will also set itself to none when you edit the entry widget from within either the -validatecommand or the -invalidcommand. such editions will override the one that was being validated. ๐ญ
if you wish to edit the entry widget (for example set it to {}) during validation and still have the -validate option set, you should include the command:
after idle {%W config -validate %v}
in the -validatecommand or -invalidcommand (whichever one you were editing the entry widget from). it's also recommended to not set an associated -textvariable during validation, because that can cause the entry widget to become out of sync with the -textvariable. ๐
(more coming, bestie โ this was just part 1 ๐)
๐ WIDGET COMMAND ๐
okay so here's the tea โ โ the entry command spins up a brand-new Tcl command, and its name is pathName. That command is your lil remote control for the widget, and you can use it to poke at all sorts of operations. General form, iconic as always:
pathName subcommand ?arg ...?
Your subcommand and the args are the whole personality here โ they decide exactly what the command does. โจ
๐ INDICES
Bestie, so many of the widget commands for entries take one or more indices as arguments. An index just points at a particular character in the entry's string, and you can name it in any of these ways:
numberโ pins down the character by numerical index, where0is the very first character in the string. ๐ฅanchorโ the anchor point for the selection, which you set with theselect fromandselect adjustwidget commands.endโ the character just after the last one in the entry's string. It's literally the same as giving a numerical index equal to the length of the entry's string. ๐insertโ the character adjacent to and immediately following the insertion cursor.sel.firstโ the first character in the selection. Heads up though: it's an error to use this form if the selection is not in the entry window. ๐ญsel.lastโ the character just after the last one in the selection. Same deal, it's an error to use this form if the selection is not in the entry window. ๐@numberโ here number gets treated as an x-coordinate in the entry's window, and whichever character spans that x-coordinate is the one you get. Like,@0indicates the left-most character in the window. ๐
You can use abbreviations for any of the forms above, e.g. "e" or "sel.f". In general, out-of-range indices are automatically rounded to the nearest legal value โ no cap, it just snaps into place. ๐ซ And indexes support the same simple interpretation as for the command string index, with simple integer index arithmetic and indexing relative to end.
๐ ๏ธ SUBCOMMANDS
The following commands are possible for entry widgets:
pathName bbox indexโ hands you a list of four numbers describing the bounding box of the character given by index. First two elements = the x and y coordinates of the upper-left corner of the screen area the character covers (in pixels, relative to the widget), and the last two elements = the width and height of the character, in pixels. Fair warning: the bounding box may refer to a region outside the visible area of the window. ๐pathName cget optionโ returns the current value of the configuration option given by option. Option can be any of the values accepted by theentrycommand. ๐pathName configure ?option? ?value option value ...?โ query or modify the configuration options of the widget. Here's the breakdown, fr fr:- No option specified โ returns a list describing all the available options for pathName (peep
Tk_ConfigureInfofor info on the format of this list). - option given with no value โ returns a list describing just that one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified).
- One or more option-value pairs specified โ modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string.
- No option specified โ returns a list describing all the available options for pathName (peep
Option can be any of the values accepted by the entry command. โจ
pathName delete first ?last?โ deletes one or more elements of the entry. First is the index of the first character to delete, and last is the index of the character just after the last one to delete. If last isn't specified it defaults tofirst+1, i.e. a single character gets deleted. Returns an empty string. bye-bye character ๐pathName getโ returns the entry's string. That's it, that's the whole vibe. ๐ซถpathName icursor indexโ arranges for the insertion cursor to be displayed just before the character given by index. Returns an empty string.pathName index indexโ returns the numerical index corresponding to index. ๐ขpathName insert index stringโ inserts the characters of string just before the character indicated by index. Returns an empty string. โ๏ธpathName scan option argsโ used to implement scanning on entries. Two forms, depending on option:pathName scan mark xโ records x and the current view in the entry window; used together with laterscan dragtocommands. Typically this command is associated with a mouse button press in the widget. Returns an empty string. ๐ฑ๏ธpathName scan dragto xโ computes the difference between its x argument and the x argument to the lastscan markcommand for the widget. Then it adjusts the view left or right by 10 times the difference in x-coordinates. Typically associated with mouse motion events in the widget, to produce that effect of dragging the entry at high speed through the window. Return value is an empty string. ๐๏ธ๐จ
pathName selection option argโ used to adjust the selection within an entry. Several forms, depending on option:pathName selection adjust indexโ locates the end of the selection nearest to the character given by index, and adjusts that end of the selection to be at index (i.e. including but not going beyond index). The other end of the selection becomes the anchor point for futureselect tocommands. If the selection isn't currently in the entry, a new selection is created including the characters between index and the most recent selection anchor point, inclusive. Returns an empty string.pathName selection clearโ clears the selection if it's currently in this widget. If the selection isn't in this widget, the command has no effect. Returns an empty string. ๐งนpathName selection from indexโ sets the selection anchor point to just before the character given by index. Does not change the selection. Returns an empty string.pathName selection presentโ returns1if there are characters selected in the entry,0if nothing is selected. (yes, the original says "if there is are characters selected" โ we love a typo moment, she's still iconic ๐ )pathName selection range start endโ sets the selection to include the characters starting with the one indexed by start and ending with the one just before end. If end refers to the same character as start or an earlier one, then the entry's selection is cleared.pathName selection to indexโ three moods:- index before the anchor point โ set the selection to the characters from index up to but not including the anchor point.
- index the same as the anchor point โ do nothing.
- index after the anchor point โ set the selection to the characters from the anchor point up to but not including index.
The anchor point is determined by the most recent select from or select adjust command in this widget. If the selection isn't in this widget, a new selection is created using the most recent anchor point specified for the widget. Returns an empty string. ๐ฆ
pathName validateโ forces an evaluation of the-validatecommandindependent of the conditions specified by the-validateoption. It does this by temporarily setting the-validateoption toall. Returns0or1. โpathName xview argsโ query and change the horizontal position of the text in the widget's window. Any of these forms:pathName xviewโ returns a list containing two elements, each a real fraction between0and1; together they describe the horizontal span that's visible in the window. Example time: if the first element is.2and the second is.6, then 20% of the entry's text is off-screen to the left, the middle 40% is visible in the window, and 40% of the text is off-screen to the right. These are the same values passed to scrollbars via the-xscrollcommandoption. ๐pathName xview indexโ adjusts the view in the window so the character given by index is displayed at the left edge of the window.pathName xview moveto fractionโ adjusts the view so the character fraction of the way through the text appears at the left edge of the window. Fraction must be a fraction between0and1.pathName xview scroll number whatโ shifts the view in the window left or right according to number and what. Number must be an integer or a float, but if it's a float then it gets converted to an integer, rounded away from 0. What must be eitherpagesorunitsor an abbreviation of one of these. If what ispagesthen the view adjusts by number screenfuls. If number is negative, characters farther to the left become visible; if it's positive, characters farther to the right become visible. If what isunits, the view adjusts left or right by number average-width characters on the display. ๐ฅ
โจ DEFAULT BINDINGS โจ
okay so this is the part everyone actually cares about ๐
Tk goes ahead and auto-creates class bindings for entries, so they just work out of the box. Here's the whole default behavior, bestie. And real quick, before we dive in: in everything below, โwordโ means a contiguous group of letters, digits, or _ characters โ or literally any single character that isn't one of those. iykyk, keep that in your back pocket ๐ซถ
- Clicking mouse button 1 positions the insertion cursor just before the character underneath the mouse cursor, sets the input focus to this widget, and clears any selection in the widget. Dragging with mouse button 1 strokes out a selection between the insertion cursor and the character under the mouse. ๐ฑ๏ธ
- Double-clicking with mouse button 1 selects the word under the mouse and positions the insertion cursor at the end of the word. Drag after a double click and you'll stroke out a selection made of whole words. iconic behavior honestly โจ
- Triple-clicking with mouse button 1 selects all of the text in the entry and parks the insertion cursor at the end of the line. yes ma'am ๐
- The ends of the selection can be adjusted by dragging with mouse button 1 while the Shift key is down. This adjusts the end of the selection that was nearest to the mouse cursor when button 1 was pressed. And if the button was double-clicked before dragging, then the selection gets adjusted in units of whole words. ๐ฆ
- Clicking mouse button 1 with the Control key down positions the insertion cursor in the entry without affecting the selection. no notes, lowkey perfect
- If any normal printing characters are typed in an entry, they get inserted at the point of the insertion cursor. shocking, I know ๐ญ
- The view in the entry can be adjusted by dragging with the middle mouse button (button 2 โ or button 3 in TkAqua, don't @ me). If the middle mouse button is clicked without moving the mouse, the selection is copied into the entry at the position of the mouse cursor. ๐
- If the mouse is dragged out of the entry on the left or right sides while button 1 is pressed, the entry will automatically scroll to make more text visible โ but only if there's more text off-screen on the side where the mouse left the window. highkey thoughtful
- The Left and Right keys move the insertion cursor one character to the left or right; they also clear any selection in the entry and set the selection anchor. If Left or Right is typed with the Shift key down, then the insertion cursor moves and the selection is extended to include the new character. Control+Left and Control+Right move the insertion cursor by words, and Control+Shift+Left and Control+Shift+Right move the insertion cursor by words and extend the selection. Control-b and Control-f behave the same as Left and Right respectively. Meta-b and Meta-f behave the same as Control+Left and Control+Right respectively. the way that's a whole keyboard workout ๐
- The Home key, or Control-a, moves the insertion cursor to the beginning of the entry and clears any selection in the entry. Shift+Home moves the insertion cursor to the beginning of the entry and extends the selection to that point. ๐
- The End key, or Control-e, moves the insertion cursor to the end of the entry and clears any selection in the entry. Shift+End moves the cursor to the end and extends the selection to that point. matching set, we love symmetry ๐
- The Select key and Control-Space set the selection anchor to the position of the insertion cursor. Note that they do not affect the current selection. Shift+Select and Control-Shift-Space adjust the selection to the current position of the insertion cursor โ selecting from the anchor to the insertion cursor if there wasn't any selection previously. ๐
- Control-/ selects all the text in the entry. that's it. that's the whole vibe. periodt โญ
- Control-\ clears any selection in the entry. the anti-select-all, we stan a balanced ecosystem ๐ธ
- The F16 key (labelled Copy on many Sun workstations) or Meta-w copies the selection in the widget to the clipboard โ if there is a selection. no selection, no copy, them's the rules ๐ง
- The F20 key (labelled Cut on many Sun workstations) or Control-w copies the selection in the widget to the clipboard and deletes the selection. If there's no selection in the widget, then these keys have no effect. nada. nothing. zip. ๐ฅ
- The F18 key (labelled Paste on many Sun workstations) or Control-y inserts the contents of the clipboard at the position of the insertion cursor. the classic ctrl-v's cooler older cousin fr fr ๐
- The Delete key deletes the selection, if there is one in the entry. If there is no selection, it deletes the character to the right of the insertion cursor. direction matters!! ๐
- The BackSpace key and Control-h delete the selection, if there is one in the entry. If there is no selection, it deletes the character to the left of the insertion cursor. the other direction. see what they did there ๐
- Control-d deletes the character to the right of the insertion cursor. ๐ฆ
- Meta-d deletes the word to the right of the insertion cursor. whole word. gone. bye bestie ๐ญ
- Control-k deletes all the characters to the right of the insertion cursor. aggressive but valid ๐
- Control-t reverses the order of the two characters to the right of the insertion cursor. chaotic little transmutation, love that for you ๐ฎ
โ ๏ธ okay important one, don't skip this: if the entry is disabled using the-stateoption, then the entry's view can still be adjusted and text in the entry can still be selected โ but no insertion cursor will be displayed and no text modifications will take place. The one exception: if the entry is linked to a variable using the-textvariableoption, then any changes to the variable are reflected by the entry, whatever the value of its-stateoption. yes, even when it's disabled. that's not a bug, that's the spec ๐ซถ
And finally โ the behavior of entries can be changed by defining new bindings for individual widgets, or by redefining the class bindings. so go off, customize, make it yours โจ
๐ SEE ALSO ๐
ttk::entry(n)โ the themed glow-up sibling, go say hi ๐
๐ KEYWORDS ๐
entry, widget โ that's the whole tag list, short and sweet ๐
and that's the whole page bestie!! you made it to the end, I'm so proud ๐ฅน this one's from Tk, version 8.3, section entry(n) โ go build something cute with it. mwah ๐โจ๐
๐ฆ where this page comes from
- alpine 3.23: tk9-doc 9.0.3-r0 ยท TCL
- alpine 3.24: tk9-doc 9.0.3-r2 ยท TCL
- fedora 43: tk 1:9.0.2-1.fc43 ยท TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 ยท TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 ยท TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 ยท TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- Copyright (c) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 1998-2000 Scriptics Corporation
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms