ttk::entry(3tk)

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

okay bestie, sit down, hydrate, and let me tell you about ttk::entry(3tk) β€” she's a Tk Themed Widget and she's literally just an editable text field. That's it. That's the girl. But she is so much more than she looks, and you're gonna be obsessed by the end of this.


✨ NAME ✨

ttk::entry β€” editable text field widget πŸ’–


🌸 SYNOPSIS 🌸

ttk::entry pathName ?options?

Yeah. That's the whole entrance. Give her a pathName, hand her some options, she slays. Periodt. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

An ttk::entry widget displays a one-line text string and lets the user edit that string. Groundbreaking, I know, but the way she does it? Immaculate.

The value of that string can be linked to a Tcl variable through the -textvariable option β€” so like, the variable and the widget are in a whole committed relationship, and changes flow both ways. πŸ’ž

Entry widgets also support horizontal scrolling with the standard -xscrollcommand option and the xview widget command. So yes, she can scroll sideways, she's versatile, she contains multitudes. 🌈


πŸŽ€ STANDARD OPTIONS πŸŽ€

Here's the roster of standard options she shows up with:

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

(yes, that last one is written exactly like that on the page. no cap, not me editing it, it's giving "the docs said what they said".)

See the ttk_widget manual entry for details on the standard options β€” that's where all the lore lives. πŸ“–


πŸ’… WIDGET-SPECIFIC OPTIONS πŸ’…

These are her options, the ones that make her her. Each one comes with a Command-Line Name, a Database Name, and a Database Class, so let's give them all their moment. ✨

  • -exportselection β€” a boolean value specifying whether or not a selection in the widget should be linked to the X selection. If the selection is exported, then selecting in 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 "we share one clipboard and we respect each other's boundaries". 🫢
    • Database Name: exportSelection
    • Database Class: ExportSelection
  • -invalidcommand β€” a script template to evaluate whenever the -validatecommand returns 0. So like, when validation says "no ma'am", this is the script that gets to react. See VALIDATION below for more info. 🚨
    • Database Name: invalidCommand
    • Database Class: InvalidCommand
  • -justify β€” specifies how the text is aligned within the entry widget. One of left, center, or right. Pick your alignment, pick your vibe. πŸ“
    • Database Name: justify
    • Database Class: Justify
  • -show β€” if this option is specified, then the true contents of the entry are not displayed in the window. Instead, each character in the entry's value will be displayed as the first character in the value of this option, such as * or a bullet. This is useful, for example, if the entry is to be used to enter a password. πŸ”’ And heads up: 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 like... the disguise is the whole thing, bestie. πŸ™ˆ
    • Database Name: show
    • Database Class: Show
  • -state β€” a compatibility option; see ttk::widget(3tk) for details. Specifies one of three states for the entry: normal, disabled, or readonly. See WIDGET STATES, below. πŸ’€
    • Database Name: state
    • Database Class: State
  • -textvariable β€” 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. They're synced, they're in their era, they're a matched set. πŸͺž
    • Database Name: textVariable
    • Database Class: Variable
  • -validate β€” specifies the mode in which validation should operate: none, focus, focusin, focusout, key, or all. Default is none, meaning that validation is disabled. See VALIDATION below. πŸ•΅οΈ
    • Database Name: validate
    • Database Class: Validate
  • -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. βš–οΈ
    • Database Name: validateCommand
    • Database Class: ValidateCommand
  • -width β€” specifies an integer value indicating the desired width of the entry window, in average-size characters of the widget's font. So it's measured in vibes-per-character, basically. πŸ“
    • Database Name: width
    • Database Class: Width

🧚 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. That blinking little line? That's her telling you "right here bestie, this is where it's going". ⭐

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). So yeah, long strings don't scare her, she just scrolls. πŸ’…


🧭 INDICES 🧭

So many of the entry widget commands take one or more indices as arguments. An index specifies a particular character in the entry's string, and you can say it in any of these ways:

number

Specifies the character as a numerical index, where 0 corresponds to the first character in the string. Zero-indexed, obviously, we're not monsters. πŸ”’

@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. So she's forgiving like that β€” she'll snap your sloppy index to the nearest valid one, no judgment. 🫢


πŸ‘‘ WIDGET COMMAND πŸ‘‘

In addition to the standard cget, configure, identify element, instate, state, style and xview commands (see ttk::widget), entry widgets support the following 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. So like, she'll tell you where a character would be, even if you can't see her right now. 🌫️

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 the empty string. πŸ—‘οΈ

pathName get

Returns the entry's string. Just hands it over. No notes. πŸ“€

pathName icursor index

Arrange 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. Translates for you, bestie. πŸ”

pathName insert index string

Insert 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. πŸ“Š

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. So backwards ranges just wipe it, she doesn't do reverse selections. πŸ™…

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. βœ…


and that's only part one, bestie β€” she's got more lore where that came from πŸ’–

πŸ’– VALIDATION πŸ’–

Okay bestie, so the entry widget can do validation and it's honestly iconic. You turn it on with -validate, -validatecommand, and -invalidcommand. That's the trio. Periodt. ✨

πŸ¦‹ VALIDATION MODES πŸ¦‹

There are two main modes, and no cap, they're giving very different energy:

  • prevalidation β€” the -validatecommand is evaluated before each edit, and whatever it returns decides whether the change gets accepted or rejected. Main character decision-making.
  • revalidation β€” the -validatecommand is evaluated to see if the current value is valid. Like a vibe check for your text. πŸ’…

The -validate option says when validation happens. It can be any of these:

  • none β€” Default. Validation only happens when you specifically ask for it with the validate widget command. Lowkey chill.
  • key β€” The entry gets prevalidated before every edit (specifically, whenever the insert or delete widget commands are called). If prevalidation fails, the edit is rejected. Bye. πŸ’€
  • focus β€” The entry is revalidated when it receives or loses focus.
  • focusin β€” The entry is revalidated when it receives focus.
  • focusout β€” The entry is revalidated when it loses focus.
  • all β€” Validation happens for all of the above conditions. She does it all. πŸ‘‘

The -invalidcommand is evaluated whenever the -validatecommand returns a false value. So if validation says "nope," -invalidcommand gets the mic. 🎀

The -validatecommand and -invalidcommand can modify the entry widget's value through the widget insert or delete commands, or by setting the linked -textvariable. But heads up: if either one does that during prevalidation, then the edit is rejected regardless of the value returned by the -validatecommand. The audacity, honestly. 🫠

If -validatecommand is empty (which is the default), validation always succeeds. So no stress. πŸ’–

🌸 VALIDATION SCRIPT SUBSTITUTIONS 🌸

You can do percent substitutions on -validatecommand and -invalidcommand, just like in a bind script. The substitutions are:

  • %d β€” Type of action: 1 for insert prevalidation, 0 for delete prevalidation, or -1 for revalidation.
  • %i β€” Index of character string to be 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 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 standard Tk entry widget automatically disables validation (by setting -validate to none) if the -validatecommand or -invalidcommand modifies the entry's value. The Tk themed entry widget? She's different. She only disables validation if one of the validation scripts raises an error, or if -validatecommand does not return a valid boolean value. (So bestie, it is not necessary to re-enable validation after modifying the entry value in a validation script. Slay.) ✨

Also: the standard entry widget invokes validation whenever the linked -textvariable is modified. The Tk themed entry widget does not. Not me pointing out the family drama. πŸ™ˆ

πŸŽ€ DEFAULT BINDINGS πŸŽ€

The entry widget's default bindings enable the following behavior. In the descriptions below, β€œword” refers to a contiguous group of letters, digits, or β€œ_” characters, or any single character other than these. 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.
  • 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.
  • The ends of the selection can be adjusted by dragging with mouse button 1 while the Shift key is down. If the button is double-clicked before dragging then the selection will be 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.
  • If any normal printing characters are typed in an entry, they are 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 is more text off-screen on the side where the mouse left the window).
  • 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 is 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 is 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 in addition text cannot be selected. So disabled is like readonly plus extra no-touch energy. 🚫

Note that changes to the linked -textvariable will still be reflected in the entry, even if it is disabled or readonly. The variable said "I'm still showing up," and honestly, same. πŸ’…

Typically, the text is β€œgrayed-out” in the disabled state, and a different background is used in the readonly state. Aesthetic choices. 🎨

The entry widget sets the invalid state if revalidation fails, and clears it whenever validation succeeds. Vibe check passed, bestie. βœ…

✨ STYLING OPTIONS ✨

The class name for a ttk::entry is TEntry. Remember that for the style girlies. πŸ‘‘

Dynamic states: disabled, focus, readonly. The moods. πŸ’–

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.
  • -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, style icon. πŸ’…

πŸ‘‘ SEE ALSO πŸ‘‘

🌈 KEYWORDS 🌈

  • entry
  • widget
  • text field

That's the page, bestie. Source: Tk, version: 8.5, page: ttk::entry(3tk), and the date is the one in the footer. Mwah. πŸ’‹βœ¨

πŸ“¦ where this page comes from