entry(3tk)
β¨ 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-disabledforegroundand-disabledbackgroundoptions.
-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-invalidcommandwill 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βvalidateCommandwill be called when the entry receives or loses focus.focusinβvalidateCommandwill be called when the entry receives focus.focusoutβvalidateCommandwill be called when the entry loses focus.keyβvalidateCommandwill be called when the entry is edited.allβvalidateCommandwill 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:1for insert,0for delete, or-1for 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 newtextvariable, 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-textvariableand-validatecommandoptions can be dangerous to mix. any problems have been overcome so that using the-validatecommandwill not interfere with the traditional behavior of the entry widget. using the-textvariablefor read-only purposes will never cause problems. the danger comes when you try to set the-textvariableto something that the-validatecommandwould not accept β which causes-validateto becomenone(and the-invalidcommandwill 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, where0is the first character in the string. πanchorβ the anchor point for the selection, which gets set with theselect fromandselect adjustwidget 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β herenumberis treated as an x-coordinate in the entry's window; the character spanning that x-coordinate is used. For example,@0indicates 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 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 cget optionβ Returns the current value of the configuration option given byoption.Optionmay have any of the values accepted by theentrycommand. πpathName configure ?option? ?value option value ...?β Query or modify the configuration options of the widget. If nooptionis specified, returns a list describing all of the available options forpathName(seeTk_ConfigureInfofor information on the format of this list). Ifoptionis specified with novalue, 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 nooptionis 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.Optionmay have any of the values accepted by theentrycommand. βοΈpathName delete first ?last?β Delete 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 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 byindex. Returns an empty string. π±οΈpathName index indexβ Returns the numerical index corresponding toindex. π’pathName insert index stringβ Insert the characters ofstringjust before the character indicated byindex. Returns an empty string. βοΈpathName scan option argsβ This command is used to implement scanning on entries. It has two forms, depending onoption:pathName scan mark xβ Recordsxand the current view in the entry window; used in conjunction with laterscan dragtocommands. 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 itsxargument and thexargument to the lastscan markcommand 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 onoption:pathName selection adjust indexβ Locate the end of the selection nearest to the character given byindex, and adjust that end of the selection to be atindex(i.e. including but not going beyondindex). The other end of the selection is made the anchor point for futureselect tocommands. If the selection is not currently in the entry, then a new selection is created to include the characters betweenindexand 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 byindex. Does not change the selection. Returns an empty string. βpathName selection presentβ Returns1if there are characters selected in the entry,0if nothing is selected. β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 selection to indexβ Ifindexis before the anchor point, set the selection to the characters fromindexup to but not including the anchor point. Ifindexis the same as the anchor point, do nothing. Ifindexis after the anchor point, set the selection to the characters from the anchor point up to but not includingindex. The anchor point is determined by the most recentselect fromorselect adjustcommand 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-validatecommandindependent of the conditions specified by the-validateoption. This is done by temporarily setting the-validateoption toall. It returns0or1. π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 between0and1; together they describe the horizontal span that is visible in the window. For example, if the first element is.2and 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-xscrollcommandoption. πpathName xview indexβ Adjusts the view in the window so that the character given byindexis displayed at the left edge of the window. πpathName xview moveto fractionβ Adjusts the view in the window so that the characterfractionof the way through the text appears at the left edge of the window.Fractionmust be a fraction between0and1. ποΈpathName xview scroll number whatβ This command shifts the view in the window left or right according tonumberandwhat.Numbermust be an integer or a float, but if it is a float then it is converted to an integer, rounded away from0.Whatmust be eitherpagesorunitsor an abbreviation of one of these. Ifwhatispagesthen the view adjusts bynumberscreenfuls. Ifnumberis negative then characters farther to the left become visible; if it is positive then characters farther to the right become visible. Ifwhatisunits, the view adjusts left or right bynumberaverage-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
LeftandRightkeys 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 pressingLeftorRight, the insertion cursor moves and the selection is extended to include the new character.Control-LeftandControl-Rightmove the insertion cursor by words, andControl-Shift-LeftandControl-Shift-Rightmove the insertion cursor by words and extend the selection.Control-bandControl-fdo the same asLeftandRight, respectively.Meta-bandMeta-fdo the same asControl-LeftandControl-Right, respectively. keyboard shortcuts for days. β¨οΈβ¨ - [10] the
Homekey, orControl-a, moves the insertion cursor to the beginning of the entry and clears any selection in the entry.Shift-Homemoves the insertion cursor to the beginning of the entry and also extends the selection to that point. home sweet home. π - [11] the
Endkey, orControl-e, moves the insertion cursor to the end of the entry and clears any selection in the entry.Shift-Endmoves the cursor to the end and extends the selection to that point. to the end and beyond. π - [12] the
Selectkey andControl-Spaceset the selection anchor to the position of the insertion cursor. they do not affect the current selection.Shift-SelectandControl-Shift-Spaceadjust 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
F16key (labelled Copy on many Sun workstations) orMeta-wcopies the selection in the widget to the clipboard, if there is a selection. copy that. π - [16] the
F20key (labelled Cut on many Sun workstations) orControl-wcopies 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
F18key (labelled Paste on many Sun workstations) orControl-yinserts the contents of the clipboard at the position of the insertion cursor. paste it, periodt. πβ¨ - [18] the
Deletekey 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
BackSpacekey andControl-hdelete 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-ddeletes the character to the right of the insertion cursor. bye bye character. π - [21]
Meta-ddeletes the word to the right of the insertion cursor. whole word gone. π - [22]
Control-kdeletes all the characters to the right of the insertion cursor. clean slate. β¨ - [23]
Control-treverses the order of the two characters to the right of the insertion cursor. flip it. π
π¨ if the entry is disabled using the-stateoption, 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-textvariableoption, in which case any changes to the variable are reflected by the entry whatever the value of its-stateoption. 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
- 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) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 1998-2000 Scriptics Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms