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, where0corresponds to the first character in the string.@numberβ In this form,numberis 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 byindex. 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.firstis the index of the first character to delete, andlastis the index of the character just after the last one to delete. Iflastis not specified it defaults tofirst+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 byindex. Returns the empty string.pathName index indexβ Returns the numerical index corresponding toindex.pathName insert index stringβ Insertsstringjust before the character indicated byindex. Returns the empty string.pathName selection option argβ This command is used to adjust the selection within an entry. It has several forms, depending onoption: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β Returns1if there is are characters selected in the entry,0if 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 bystartand ending with the one just beforeend. Ifendrefers to the same character asstartor an earlier one, then the entryβs selection is cleared.
pathName validateβ Force revalidation, independent of the conditions specified by the-validateoption. Returns0if validation fails,1if 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
-validatecommandgets 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
-validatecommandis 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 thevalidatewidget command. Lowkey chill mode.keyβ the entry gets prevalidated before every single edit (specifically, whenever theinsertordeletewidget 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:-validatecommandand-invalidcommandare allowed to modify the entry widget's value via the widgetinsertordeletecommands, or by setting the linked-textvariable. But if either of them does that during prevalidation, the edit gets rejected regardless of what the-validatecommandreturned. 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:1for insert prevalidation,0for delete prevalidation, or-1for 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-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 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-bandControl-fbehave the same as Left and Right, respectively. - The Home key and
Control-amove 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-emove 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-hdelete 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-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 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:
-backgroundcolor β for backwards compatibility, when using theaquatheme (for macOS), this option behaves as an alias for the-fieldbackground, provided 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. Sad but true.-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. π
π SEE ALSO π
ttk::widget(n)entry(n)
β 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
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- 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-1017 on : a language model's rewrite of the original, under its terms