ttk::entry(n)

✨ NAME ✨

ttk::entry β€” Editable text field widget. This is the ttk::entry(n) manual page, bestie, and its category vibe is Tk Themed Widget πŸ’….

🌸 SYNOPSIS 🌸

ttk::entry pathName ?options?

πŸŽ€ DESCRIPTION πŸŽ€

A ttk::entry widget shows a one-line text string and lets the user edit it. The string’s value can be linked to a Tcl variable with the -textvariable option. Entry widgets support horizontal scrolling with the standard -xscrollcommand option and the xview widget command. Slay, it scrolls πŸ’–.

πŸ’… STANDARD OPTIONS πŸ’…

The standard-options lineup is exactly this crew:

  • -class
  • -cursor
  • -font
  • -foreground
  • -style
  • -takefocus
  • -xscrollcommand
  • -placeholder-placeholderforeground

See the ttk_widget manual entry for details on the standard options.

πŸ¦‹ WIDGET-SPECIFIC OPTIONS πŸ¦‹

πŸŽ€ -exportselection

Command-Line Name: -exportselection Database Name: exportSelection Database Class: ExportSelection

A boolean value that says whether a selection in this widget gets linked to the X selection. If the selection is exported, then selecting inside the widget deselects the current X selection, selecting outside the widget deselects any widget selection, and the widget will respond to selection retrieval requests when it has a selection. It’s giving selection diplomacy ✨.

πŸŽ€ -invalidcommand

Command-Line Name: -invalidcommand Database Name: invalidCommand Database Class: InvalidCommand

A script template to evaluate whenever the -validatecommand returns 0. See VALIDATION below for more info. This is the β€œoops, invalid” script, bestie.

πŸŽ€ -justify

Command-Line Name: -justify Database Name: justify Database Class: Justify

Specifies how the text is aligned inside the entry widget. One of left, center, or right.

πŸŽ€ -show

Command-Line Name: -show Database Name: show Database Class: Show

If this option is set, then the true contents of the entry are not displayed in the window. Instead, each character in the entry’s value is displayed as the first character in the value of this option, like β€œ*” or a bullet. This is useful, for example, if the entry is used to enter a password. If characters in the entry are selected and copied elsewhere, the information copied will be what is displayed, not the true contents of the entry. So yes, password privacy is the moment, but the copied text follows the display vibe πŸ’€.

πŸŽ€ -state

Command-Line Name: -state Database Name: state Database Class: State

Compatibility option; see ttk::widget(n) for details. Specifies one of three states for the entry: normal, disabled, or readonly. See WIDGET STATES, below.

πŸŽ€ -textvariable

Command-Line Name: -textvariable Database Name: textVariable Database Class: Variable

Specifies the name of a global variable whose value is linked to the entry widget’s contents. Whenever the variable changes value, the widget’s contents are updated, and vice versa. It’s a two-way bestie connection πŸ”₯.

πŸŽ€ -validate

Command-Line Name: -validate Database Name: validate Database Class: Validate

Specifies the mode in which validation should operate: none, focus, focusin, focusout, key, or all. Default is none, meaning validation is disabled. See VALIDATION below.

πŸŽ€ -validatecommand

Command-Line Name: -validatecommand Database Name: validateCommand Database Class: ValidateCommand

A script template to evaluate whenever validation is triggered. If set to the empty string (the default), validation is disabled. The script must return a boolean value. See VALIDATION below.

πŸŽ€ -width

Command-Line Name: -width Database Name: width Database Class: Width

Specifies an integer value indicating the desired width of the entry window, in average-size characters of the widget’s font.

🌈 NOTES 🌈

A portion of the entry may be selected as described below. If an entry is exporting its selection (see the -exportselection option), then it will observe 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 insert cursor to indicate where new characters will be inserted. Iconic focus behavior, no cap.

Entries are capable of displaying strings that are too long to fit entirely within the widget’s window. In this case, only a portion of the string will be displayed; 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). Horizontal scrolling ate, periodt.

⭐ INDICES ⭐

Many of the entry widget commands take one or more indices as arguments. An index specifies a particular character in the entry’s string, in any of these ways:

  • number β€” Specifies the character as a numerical index, where 0 corresponds to the first character in the string.
  • @number β€” In this form, 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.
  • end β€” Indicates the character just after the last one in the entry’s string. This is equivalent to specifying a numerical index equal to the length of the entry’s string.
  • insert β€” Indicates the character adjacent to and immediately following the insert cursor.
  • sel.first β€” Indicates 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 β€” Indicates 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.

Abbreviations may be used for any of the forms above, e.g. β€œe” or β€œsel.l”. 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.

πŸ‘‘ WIDGET COMMAND πŸ‘‘

In addition to the standard cget, configure, identify element, instate, state, style and xview commands (see ttk::widget), entry widgets support these additional commands:

  • 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 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 is not specified it defaults to first+1, i.e. a single character is deleted. This command returns the empty string.
  • pathName get β€” Returns the entry’s string.
  • pathName icursor index β€” Arranges for the insert cursor to be displayed just before the character given by index. Returns the empty string.
  • pathName index index β€” Returns the numerical index corresponding to index.
  • pathName insert index string β€” Inserts string just before the character indicated by index. Returns the 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 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 the empty string.
    • pathName selection present β€” Returns 1 if there is are characters selected in the entry, 0 if nothing is selected. (Yes, the manpage says β€œthere is are”; I’m keeping the original energy πŸ’€.)
    • 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 validate β€” Force revalidation, independent of the conditions specified by the -validate option. Returns 0 if validation fails, 1 if it succeeds. Sets or clears the invalid state accordingly. See VALIDATION below for more details.

πŸ’– VALIDATION πŸ’–

Okay bestie, this is the part where the entry widget gets opinionated about what you're allowed to type. The -validate, -validatecommand, and -invalidcommand options are what turn on entry widget validation. Slay.

🌸 VALIDATION MODES 🌸

There are two main modes, and yes you need to know both:

  • prevalidation β€” the -validatecommand gets evaluated prior to each edit, and whatever it returns decides whether the change is accepted or rejected. It's giving bouncer at the club.
  • revalidation β€” the -validatecommand is evaluated to figure out whether the current value is even valid. Vibes check on what's already there.

The -validate option is the one that decides when validation happens. It can be any of these:

  • none β€” the default. Validation only happens when you specifically ask for it with the validate widget command. Lowkey chill mode.
  • key β€” the entry gets prevalidated before every single edit (specifically, whenever the insert or delete widget commands are called). If prevalidation fails, the edit is rejected. No cap.
  • focus β€” the entry is revalidated whenever it gains or loses focus.
  • focusin β€” revalidated when the entry receives focus.
  • focusout β€” revalidated when the entry loses focus.
  • all β€” validation runs for all of the conditions above. Main character energy, does everything.

The -invalidcommand gets evaluated whenever the -validatecommand returns a false value. That's your "uh uh, try again" handler.

⚠️ Heads up: -validatecommand and -invalidcommand are allowed to modify the entry widget's value via the widget insert or delete commands, or by setting the linked -textvariable. But if either of them does that during prevalidation, the edit gets rejected regardless of what the -validatecommand returned. Periodt.

If -validatecommand is empty (which is the default), validation always succeeds. Easy.

πŸŽ€ VALIDATION SCRIPT SUBSTITUTIONS πŸŽ€

You can do percent substitutions on -validatecommand and -invalidcommand, literally just like in a bind script. These are the recognized ones:

  • %d β€” type of action: 1 for insert prevalidation, 0 for delete prevalidation, or -1 for revalidation.
  • %i β€” index of the character string being inserted/deleted, if any, otherwise -1.
  • %P β€” in prevalidation, the new value of the entry if the edit is accepted. In revalidation, the current value of the entry.
  • %s β€” the current value of the entry prior to editing.
  • %S β€” the text string being inserted/deleted, if any, {} otherwise.
  • %v β€” the current value of the -validate option.
  • %V β€” the validation condition that triggered the callback (key, focusin, focusout, or forced).
  • %W β€” the name of the entry widget.

πŸ’… DIFFERENCES FROM TK ENTRY WIDGET VALIDATION πŸ’…

So the classic Tk entry widget is kinda dramatic: it automatically disables validation (by setting -validate to none) if the -validatecommand or -invalidcommand modifies the entry's value. The Tk themed entry widget? Way more grown-up about it. It only disables validation if one of the validation scripts raises an error, or if -validatecommand doesn't return a valid boolean value. (So basically: you do not need to re-enable validation after modifying the entry value inside a validation script. You're welcome.)

Also β€” and this is the other big one β€” the standard entry widget invokes validation whenever the linked -textvariable is modified. The Tk themed entry widget does not. Noted. πŸ’€


✨ DEFAULT BINDINGS ✨

The entry widget's default bindings enable all this behavior. In the descriptions below, "word" means a contiguous group of letters, digits, or _ characters β€” or any single character other than those. Iykyk.

  • Clicking mouse button 1 positions the insert 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 down strokes out a selection between the insert cursor and the character under the mouse.
  • Double-clicking with mouse button 1 selects the word under the mouse and positions the insert cursor at the end of the word. Dragging after a double-click strokes out a selection consisting of whole words. Obsessed.
  • Triple-clicking with mouse button 1 selects all of the text in the entry and positions the insert cursor at the end of the line. Iconic.
  • The ends of the selection can be adjusted by dragging with mouse button 1 while the Shift key is down. 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 will position the insert cursor in the entry without affecting the selection. Sneaky, love that.
  • If any normal printing characters are typed in an entry, they get inserted at the point of the insert cursor.
  • The view in the entry can be adjusted by dragging with the middle mouse button (button 2, or button 3 in TkAqua). 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 (if there's more text off-screen on the side where the mouse left the window). It's giving considerate.
  • The Left and Right keys move the insert cursor one character to the left or right; they also clear any selection in the entry. 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 insert cursor by words, and Control+Shift+Left and Control+Shift+Right move the insert cursor by words and also extend the selection. Control-b and Control-f behave the same as Left and Right, respectively.
  • The Home key and Control-a move the insert cursor to the beginning of the entry and clear any selection in the entry. Shift+Home moves the insert cursor to the beginning of the entry and extends the selection to that point.
  • The End key and Control-e move the insert cursor to the end of the entry and clear any selection in the entry. Shift+End moves the cursor to the end and extends the selection to that point.
  • Control-/ selects all the text in the entry. 🫢
  • Control-\ clears any selection in the entry.
  • The standard Tk <<Cut>>, <<Copy>>, <<Paste>>, and <<Clear>> virtual events operate on the selection in the expected manner.
  • 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 insert cursor.
  • 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 insert cursor.
  • Control-d deletes the character to the right of the insert cursor.
  • Control-k deletes all the characters to the right of the insertion cursor.

πŸ¦‹ WIDGET STATES πŸ¦‹

In the disabled state, the entry cannot be edited and the text cannot be selected. In the readonly state, no insert cursor is displayed and the entry cannot be edited (specifically: the insert and delete commands have no effect). The disabled state is the same as readonly, and on top of that, text cannot be selected.

πŸ’‘ Note that changes to the linked -textvariable will still be reflected in the entry, even if it's disabled or readonly. The variable said what it said.

Typically, the text is "grayed-out" in the disabled state, and a different background is used in the readonly state.

The entry widget sets the invalid state if revalidation fails, and clears it whenever validation succeeds. Glow-up and glow-down, basically.


🌈 STYLING OPTIONS 🌈

The class name for a ttk::entry is TEntry.

Dynamic states: disabled, focus, readonly.

TEntry styling options configurable with ttk::style are:

  • -background color β€” for backwards compatibility, when using the aqua theme (for macOS), this option behaves as an alias for the -fieldbackground, provided that no value is specified for -fieldbackground. Otherwise it is ignored.
  • -bordercolor color
  • -darkcolor color
  • -fieldbackground color β€” some themes use a graphical background and their field background colors cannot be changed. Sad but true.
  • -foreground color
  • -insertcolor color
  • -insertwidth amount
  • -lightcolor color
  • -padding padding
  • -placeholderforeground color
  • -relief relief
  • -selectbackground color
  • -selectborderwidth amount
  • -selectforeground color

See the ttk::style manual page for information on how to configure ttk styles. Go off. πŸ’œ


πŸ‘‘ SEE ALSO πŸ‘‘

⭐ KEYWORDS ⭐

entry, widget, text field


And that's the whole page, bestie! πŸŽ€ This one's from the Tk 8.5 era, and the page goes by ttk::entry(n) β€” go forth and validate responsibly. πŸ’–βœ¨

πŸ“¦ where this page comes from