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 name disabledBackground, database class DisabledBackground

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 name disabledForeground, database class DisabledForeground

same energy but for the foreground color when the entry is disabled. empty string? normal foreground color, no cap. โœจ

  • -invalidcommand or -invcmd โ€” database name invalidCommand, database class InvalidCommand

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 name readonlyBackground, database class ReadonlyBackground

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 name show, database class Show

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 name state, database class State

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 -disabledforeground and -disabledbackground options. very much "you can look but don't touch" energy. ๐Ÿ’€
  • -validate โ€” database name validate, database class Validate

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. ๐Ÿ”ฅ

  • -validatecommand or -vcmd โ€” database name validateCommand, database class ValidateCommand

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 -invalidcommand gets evaluated if it's set.
  • returns 1? the new edition happens. periodt.

see VALIDATION below for the deep dive. โœ…

  • -width โ€” database name width, database class Width

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 โ€” validateCommand will be called when the entry receives or loses focus.
  • focusin โ€” validateCommand will be called when the entry receives focus.
  • focusout โ€” validateCommand will be called when the entry loses focus.
  • key โ€” validateCommand will be called when the entry is edited. โŒจ๏ธ
  • all โ€” validateCommand will 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: 1 for insert, 0 for delete, or -1 for 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 new textvariable, this will be the value of that textvariable.
  • %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 -textvariable and -validatecommand options can be dangerous to mix. the problems have been overcome so that using -validatecommand will not interfere with the traditional behavior of the entry widget. using the -textvariable for read-only purposes will never cause problems. the danger comes when you try to set the -textvariable to something that the -validatecommand would not accept โ€” that causes -validate to become none (and the -invalidcommand will 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, where 0 is the very first character in the string. ๐Ÿฅ‡
  • anchor โ€” the anchor point for the selection, which you set with the select from and select adjust widget 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, @0 indicates 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 the entry command. ๐ŸŽ€
  • 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_ConfigureInfo for 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.

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 to first+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 later scan dragto commands. 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 last scan mark command 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 future select to commands. 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 โ€” returns 1 if there are characters selected in the entry, 0 if 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 -validatecommand independent of the conditions specified by the -validate option. It does this by temporarily setting the -validate option to all. Returns 0 or 1. โœ…
  • 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 between 0 and 1; together they describe the horizontal span that's visible in the window. Example time: if the first element is .2 and 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 -xscrollcommand option. ๐Ÿ“Š
    • 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 between 0 and 1.
    • 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 either pages or units or an abbreviation of one of these. If what is pages then 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 is units, 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 ๐Ÿซถ

  1. 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. ๐Ÿ–ฑ๏ธ
  2. 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 โœจ
  3. 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 ๐Ÿ‘‘
  4. 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. ๐Ÿฆ‹
  5. Clicking mouse button 1 with the Control key down positions the insertion cursor in the entry without affecting the selection. no notes, lowkey perfect
  6. If any normal printing characters are typed in an entry, they get inserted at the point of the insertion cursor. shocking, I know ๐Ÿ˜ญ
  7. 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. ๐Ÿ“
  8. 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
  9. 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 ๐Ÿ’€
  10. 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. ๐ŸŽ€
  11. 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 ๐Ÿ’–
  12. 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. ๐Ÿ™ˆ
  13. Control-/ selects all the text in the entry. that's it. that's the whole vibe. periodt โญ
  14. Control-\ clears any selection in the entry. the anti-select-all, we stan a balanced ecosystem ๐ŸŒธ
  15. 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 ๐Ÿงš
  16. 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. ๐Ÿ”ฅ
  17. 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 ๐Ÿ‘‘
  18. 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!! ๐ŸŒˆ
  19. 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 ๐Ÿ’…
  20. Control-d deletes the character to the right of the insertion cursor. ๐Ÿฆ‹
  21. Meta-d deletes the word to the right of the insertion cursor. whole word. gone. bye bestie ๐Ÿ˜ญ
  22. Control-k deletes all the characters to the right of the insertion cursor. aggressive but valid ๐Ÿ’€
  23. 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 -state option, 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 -textvariable option, then any changes to the variable are reflected by the entry, whatever the value of its -state option. 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