ttk::entry(3tk)
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
- Database Name:
-invalidcommandβ a script template to evaluate whenever the-validatecommandreturns0. 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
- Database Name:
-justifyβ specifies how the text is aligned within the entry widget. One ofleft,center, orright. Pick your alignment, pick your vibe. π- Database Name:
justify - Database Class:
Justify
- Database Name:
-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
- Database Name:
-stateβ a compatibility option; seettk::widget(3tk)for details. Specifies one of three states for the entry:normal,disabled, orreadonly. See WIDGET STATES, below. π- Database Name:
state - Database Class:
State
- Database Name:
-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
- Database Name:
-validateβ specifies the mode in which validation should operate:none,focus,focusin,focusout,key, orall. Default isnone, meaning that validation is disabled. See VALIDATION below. π΅οΈ- Database Name:
validate - Database Class:
Validate
- Database Name:
-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
- Database Name:
-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
- Database Name:
π§ 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
-validatecommandis evaluated before each edit, and whatever it returns decides whether the change gets accepted or rejected. Main character decision-making. - revalidation β the
-validatecommandis 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 thevalidatewidget command. Lowkey chill.keyβ The entry gets prevalidated before every edit (specifically, whenever theinsertordeletewidget 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:1for insert prevalidation,0for delete prevalidation, or-1for 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-validateoption.%Vβ The validation condition that triggered the callback (key,focusin,focusout, orforced).%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
1positions 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 button1down strokes out a selection between the insert cursor and the character under the mouse. - Double-clicking with mouse button
1selects 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
1selects 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
1while theShiftkey is down. If the button is double-clicked before dragging then the selection will be adjusted in units of whole words. - Clicking mouse button
1with theControlkey 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 button3in 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
1is 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
LeftandRightkeys move the insert cursor one character to the left or right; they also clear any selection in the entry. IfLeftorRightis typed with theShiftkey down, then the insertion cursor moves and the selection is extended to include the new character.Control-LeftandControl-Rightmove the insert cursor by words, andControl-Shift-LeftandControl-Shift-Rightmove the insert cursor by words and also extend the selection.Control-bandControl-fbehave the same asLeftandRight, respectively. - The
Homekey andControl-amove the insert cursor to the beginning of the entry and clear any selection in the entry.Shift-Homemoves the insert cursor to the beginning of the entry and extends the selection to that point. - The
Endkey andControl-emove the insert cursor to the end of the entry and clear any selection in the entry.Shift-Endmoves 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
Deletekey 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
BackSpacekey andControl-hdelete 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-ddeletes the character to the right of the insert cursor.Control-kdeletes 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:
-backgroundcolor β For backwards compatibility, when using theaquatheme (for macOS), this option behaves as an alias for the-fieldbackgroundprovided that no value is specified for-fieldbackground. Otherwise it is ignored.-bordercolorcolor-darkcolorcolor-fieldbackgroundcolor β Some themes use a graphical background and their field background colors cannot be changed.-foregroundcolor-insertcolorcolor-insertwidthamount-lightcolorcolor-paddingpadding-placeholderforegroundcolor-reliefrelief-selectbackgroundcolor-selectborderwidthamount-selectforegroundcolor
See the ttk::style manual page for information on how to configure ttk styles. Go off, style icon. π
π SEE ALSO π
ttk::widget(3tk)entry(3tk)
π KEYWORDS π
entrywidgettext 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
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 1998-2000 Scriptics Corporation
- Copyright (c) 2004 Joe English
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms