entry(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ entry β€” the manual page, bestie edition πŸ’…

okay so you wanna know about entry, the Tk built-in command that makes those little one-line text boxes. this is part 1 of the page, so don't go anywhere, there's more coming 🫢


🌸 NAME 🌸

entry β€” creates and manages entry widgets, aka the one-line text entry boxes. you know, the little box you type your name into. iconic behavior.


πŸŽ€ SYNOPSIS πŸŽ€

entry pathName ?options?

πŸ‘‘ STANDARD OPTIONS πŸ‘‘

these are the standard ones, all living their best lives:

  • -background
  • -borderwidth
  • -cursor
  • -exportselection
  • -font
  • -foreground
  • -highlightbackground
  • -highlightcolor
  • -highlightthickness
  • -insertbackground
  • -insertborderwidth
  • -insertofftime
  • -insertontime
  • -insertwidth
  • -justify
  • -relief
  • -placeholder
  • -placeholderforeground
  • -selectbackground
  • -selectborderwidth
  • -selectforeground
  • -takefocus
  • -textvariable
  • -xscrollcommand

see the options manual entry for the full tea on the standard options.


πŸ’– WIDGET-SPECIFIC OPTIONS πŸ’–

these ones are the entry-only specials. main character options, fr.


-disabledbackground

  • Database Name: disabledBackground
  • Database Class: DisabledBackground

sets the background color for when the entry is disabled. if this option is the empty string, then the normal background color gets used instead. no drama.


-disabledforeground

  • Database Name: disabledForeground
  • Database Class: DisabledForeground

sets the foreground color for when the entry is disabled. if this option is the empty string, the normal foreground color is used. slay.


-invalidcommand or -invcmd

  • Database Name: invalidCommand
  • Database Class: InvalidCommand

this one specifies a script to eval when -validatecommand returns 0. setting it to {} disables the feature entirely (and that's the default, bestie). the best use of this option is to set it to bell. see VALIDATION below for more info.


-readonlybackground

  • Database Name: readonlyBackground
  • Database Class: ReadonlyBackground

sets the background color for when the entry is readonly. empty string? then the normal background color is used. simple as that.


-show

  • Database Name: show
  • Database Class: Show

if you specify this option, the true contents of the entry do NOT get displayed in the window πŸ‘€. instead, every character in the entry's value gets displayed as the first character in the value of this option β€” like β€œ*” for example. this is lowkey perfect if the entry is being used to enter a password. heads up though: if characters in the entry are selected and copied elsewhere, what gets copied is what is displayed, not the true contents of the entry. so like. be careful, okay?


-state

  • Database Name: state
  • Database Class: State

specifies one of three states for the entry: normal, disabled, or readonly.

  • if the entry is 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. the contents can still be selected though πŸ’…
  • if the entry is disabled, the value may not be changed, no insertion cursor will be displayed, the contents will not be selectable, and the entry may be displayed in a different color depending on the values of the -disabledforeground and -disabledbackground options.

-validate

  • Database Name: validate
  • Database Class: Validate

specifies the mode in which validation should operate: none, focus, focusin, focusout, key, or all. it defaults to none. so if you want validation, you have to explicitly say which mode you want. no mind reading here. see VALIDATION below for more.


-validatecommand or -vcmd

  • Database Name: validateCommand
  • Database Class: ValidateCommand

specifies a script to eval when you want to validate the input into the entry widget. setting it to {} disables this feature (that's the default). this command must return a valid Tcl boolean value.

  • returns 0 (or the valid Tcl boolean equivalent)? that means you reject the new edition, it will not occur, and -invalidcommand will be evaluated if it's set.
  • returns 1? then the new edition happens. periodt.

see VALIDATION below for more info.


-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. adaptive queen πŸ¦‹


πŸ¦‹ DESCRIPTION πŸ¦‹

the entry command creates a new window (given by the pathName argument) and makes it into an entry widget. additional options, described above, may be specified on the command line or in the option database to configure aspects of the entry β€” colors, font, relief, all the aesthetics. the entry command returns its pathName argument.

at the time this command is invoked, there must not already exist a window named pathName, but pathName's parent must exist. house rules.

an entry is 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, as described below. if an entry is exporting its selection (see the -exportselection option), then it observes the standard X11 protocols for handling the selection, and 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. so like, the blinking line. you know the one.

entries are 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 will be displayed, and commands described below may be used to change the view in the window. entries use the standard -xscrollcommand mechanism for interacting with scrollbars (see the description of the -xscrollcommand option for details). they also support scanning, as described below. multitasking icons ✨


🌈 VALIDATION 🌈

validation works by setting the -validatecommand option to a script (validateCommand) which will be evaluated according to the -validate option, like this:

  • none β€” default. this means no validation will occur. vibes only, no rules.
  • 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. overachiever πŸ’…

it's 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.
⚠️ okay so listen: in general, the -textvariable and -validatecommand options can be dangerous to mix. any problems have been overcome so that using the -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 β€” which causes -validate to become none (and the -invalidcommand will not be triggered). the same thing happens when an error occurs evaluating the -validatecommand.

primarily, an error will occur 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, since that can cause the entry widget to become out of sync with the -textvariable. and nobody wants an out-of-sync widget, bestie. πŸ’€


that's part 1! more parts follow, so stick around πŸ’–

πŸ’– WIDGET COMMAND πŸ’–

okay so when you call the entry command, it makes a brand-new Tcl command named pathName. That command is your little remote control for the widget β€” you use it to run all kinds of operations. The general form is:

pathName subcommand ?arg ...?

The subcommand and the args decide exactly what the command does. No cap. ✨


🌸 INDICES 🌸

A bunch of the widget commands for entries take one or more indices as arguments. An index points to a particular character in the entry's string. You can specify it in any of these ways:

  • number β€” the character as a numerical index, where 0 is the first character in the string. πŸ“
  • anchor β€” the anchor point for the selection, which gets set with the select from and select adjust widget commands. βš“
  • end β€” the character just after the last one in the entry's string. This is the same as specifying 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. It is 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. It is an error to use this form if the selection is not in the entry window. 🚨
  • @number β€” here number is treated as an x-coordinate in the entry's window; the character spanning that x-coordinate is used. For example, @0 indicates the left-most character in the window. πŸ“

Abbreviations may be used 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. 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 β€” Returns a list of four numbers describing the bounding box of the character given by index. The first two elements of the list give the x and y coordinates of the upper-left corner of the screen area covered by the character (in pixels relative to the widget) and the last two elements give the width and height of the character, in pixels. 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 may have any of the values accepted by the entry command. πŸ”
  • pathName configure ?option? ?value option value ...? β€” Query or modify the configuration options of the widget. If no option is specified, returns a list describing all of the available options for pathName (see Tk_ConfigureInfo for information on the format of this list). If option is specified with no value, then the command returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified). If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. Option may have any of the values accepted by the entry command. βš™οΈ
  • pathName delete first ?last? β€” Delete 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 is not specified it defaults to first+1, i.e. a single character is deleted. This command returns an empty string. πŸ—‘οΈ
  • pathName get β€” Returns the entry's string. πŸ“
  • pathName icursor index β€” Arrange 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 β€” Insert the characters of string just before the character indicated by index. Returns an empty string. ✍️
  • pathName scan option args β€” This command is used to implement scanning on entries. It has two forms, depending on option:
    • pathName scan mark x β€” Records x and the current view in the entry window; used in conjunction with later scan dragto commands. Typically this command is associated with a mouse button press in the widget. It returns an empty string. πŸ–±οΈ
    • pathName scan dragto x β€” This command computes the difference between its x argument and the x argument to the last scan mark command for the widget. It then adjusts the view left or right by 10 times the difference in x-coordinates. This command is typically associated with mouse motion events in the widget, to produce the effect of dragging the entry at high speed through the window. The return value is an empty string. 🏎️
  • pathName selection option arg β€” This command is used to adjust the selection within an entry. It has several forms, depending on option:
    • pathName selection adjust index β€” Locate the end of the selection nearest to the character given by index, and adjust that end of the selection to be at index (i.e. including but not going beyond index). The other end of the selection is made the anchor point for future select to commands. If the selection is not currently in the entry, then a new selection is created to include the characters between index and the most recent selection anchor point, inclusive. Returns an empty string. 🎯
    • pathName selection clear β€” Clear the selection if it is currently in this widget. If the selection is not in this widget then the command has no effect. Returns an empty string. 🧼
    • pathName selection from index β€” Set 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. βœ…
    • 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 β€” If index is before the anchor point, set the selection to the characters from index up to but not including the anchor point. If index is the same as the anchor point, do nothing. If index is 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 is not in this widget then a new selection is created using the most recent anchor point specified for the widget. Returns an empty string. πŸͺ„
  • pathName validate β€” This command is used to force an evaluation of the -validatecommand independent of the conditions specified by the -validate option. This is done by temporarily setting the -validate option to all. It returns 0 or 1. πŸ”Ž
  • pathName xview args β€” This command is used to query and change the horizontal position of the text in the widget's window. It can take any of the following forms:
    • pathName xview β€” Returns a list containing two elements. Each element is a real fraction between 0 and 1; together they describe the horizontal span that is visible in the window. For example, if the first element is .2 and the second element is .6, 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 that the character given by index is displayed at the left edge of the window. πŸ‘ˆ
    • pathName xview moveto fraction β€” Adjusts the view in the window so that 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 β€” This command 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 is a float then it is 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 then characters farther to the left become visible; if it is positive then 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 bestie, Tk automatically creates class bindings for entries that give them all these default behaviors. and just so we're clear: when we say β€œword” down below, we mean a contiguous group of letters, digits, or _ characters, or any single character that isn't one of those. got it? πŸ’–

  • [1] clicking mouse button 1 puts the insertion cursor just before the character underneath your 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. it's giving precision. πŸ–±οΈ
  • [2] double-clicking with mouse button 1 selects the word under the mouse and puts the insertion cursor at the end of the word. and if you drag after a double click, you'll stroke out a selection made of whole words. word. ✨
  • [3] triple-clicking with mouse button 1 selects all the text in the entry and puts the insertion cursor at the end of the line. main character energy. πŸ‘‘
  • [4] you can adjust the ends of the selection by dragging with mouse button 1 while holding Shift. this adjusts the end of the selection that was nearest to the mouse cursor when button 1 was pressed. and if you double-click before dragging, the selection adjusts in units of whole words. fancy. πŸ’…
  • [5] clicking mouse button 1 with Control held down positions the insertion cursor in the entry without affecting the selection. sneaky but useful. πŸ™ˆ
  • [6] typing any normal printing characters inserts them at the point of the insertion cursor. as you do. ⌨️
  • [7] you can adjust the view in the entry by dragging with the middle mouse button (button 2, or button 3 in TkAqua). if you click the middle mouse button without moving the mouse, the selection is copied into the entry at the position of the mouse cursor. no cap. πŸ–±οΈ
  • [8] if you drag the mouse 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. auto-scroll slay. πŸ”₯
  • [9] the Left and Right keys move the insertion cursor one character left or right; they also clear any selection in the entry and set the selection anchor. if you hold Shift while pressing Left or Right, 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 do the same as Left and Right, respectively. Meta-b and Meta-f do the same as Control-Left and Control-Right, respectively. keyboard shortcuts for days. ⌨️✨
  • [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 also extends the selection to that point. home sweet home. 🏠
  • [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. to the end and beyond. πŸš€
  • [12] the Select key and Control-Space set the selection anchor to the position of the insertion cursor. 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 before. anchor management. βš“
  • [13] Control-/ selects all the text in the entry. select all, slay all. πŸ’…
  • [14] Control-\ clears any selection in the entry. bye selection. πŸ‘‹
  • [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. copy that. πŸ“‹
  • [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, these keys have no effect. cut and paste realness. βœ‚οΈ
  • [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. paste it, periodt. πŸ“‹βœ¨
  • [18] the Delete key deletes the selection, if there is one in the entry. if there's no selection, it deletes the character to the right of the insertion cursor. delete it. πŸ—‘οΈ
  • [19] the BackSpace key and Control-h delete the selection, if there is one in the entry. if there's no selection, it deletes the character to the left of the insertion cursor. backspace backspace. ⌫
  • [20] Control-d deletes the character to the right of the insertion cursor. bye bye character. πŸ‘‹
  • [21] Meta-d deletes the word to the right of the insertion cursor. whole word gone. πŸ’€
  • [22] Control-k deletes all the characters to the right of the insertion cursor. clean slate. ✨
  • [23] Control-t reverses the order of the two characters to the right of the insertion cursor. flip it. πŸ”„
🚨 if the entry is disabled using the -state option, 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 β€” except if the entry is linked to a variable using the -textvariable option, in which case any changes to the variable are reflected by the entry whatever the value of its -state option. so yeah, disabled doesn't mean completely frozen. πŸ’…

and you can change the behavior of entries by defining new bindings for individual widgets or by redefining the class bindings. you're the main character, bestie. πŸ‘‘


🌈 SEE ALSO 🌈

  • ttk::entry(3tk)

πŸŽ€ KEYWORDS πŸŽ€

  • entry
  • widget

this page is from Tk 8.3, entry(3tk), and that's the whole vibe. periodt. πŸ’–βœ¨

πŸ“¦ where this page comes from