back next

The tkinter Entry Widget

The Entry widget is a standard tkinter widget used to enter or display a single line of text.

When to use the Entry Widget

The entry widget is used to enter text strings. This widget allows the user to enter one line of text, in a single font.

To enter multiple lines of text, use the Text widget.

Patterns #

To add entry text to the widget, use the insert method. To replace the current text, you can call delete before you insert the new text.

e = Entry(master)

e.delete(0, END)
e.insert(0, "a default value")

To fetch the current entry text, use the get method:

s = e.get()

You can also bind the entry widget to a StringVar instance, and set or get the entry text via that variable:

v = StringVar()
e = Entry(master, textvariable=v)

v.set("a default value")
s = v.get()

This example creates an Entry widget, and a Button that prints the current contents:

from tkinter import *

master = Tk()

e = Entry(master)


def callback():
    print e.get()

b = Button(master, text="get", width=10, command=callback)

e = Entry(master, width=50)

text = e.get()
def makeentry(parent, caption, width=None, **options):
    Label(parent, text=caption).pack(side=LEFT)
    entry = Entry(parent, **options)
    if width:
    return entry

user = makeentry(parent, "User name:", 10)
password = makeentry(parent, "Password:", 10, show="*")
content = StringVar()
entry = Entry(parent, text=caption, textvariable=content)

text = content.get()

FIXME: More patterns to be added.

In newer versions, the Entry widget supports custom events. Document them, and add examples showing how to bind them.

Add ValidateEntry subclass as an example?



The Entry widget allows you to specify character positions in a number of ways:

  • Numerical indexes
  • END
  • Mouse coordinates (“@x”)

Numerical indexes work just like Python list indexes. The characters in the string are numbered from 0 and upwards. You specify ranges just like you slice lists in Python: for example, (0, 5) corresponds to the first five characters in the entry widget.

ANCHOR (or the string “anchor”) corresponds to the start of the selection, if any. You can use the select_from method to change this from the program.

END (or “end”) corresponds to the position just after the last character in the entry widget. The range (0, END) corresponds to all characters in the widget.

INSERT (or “insert”) corresponds to the current position of the text cursor. You can use the icursor method to change this from the program.

Finally, you can use the mouse position for the index, using the following syntax:

    "@%d" % x

where x is given in pixels relative to the left edge of the entry widget.

Reference #

Entry(master=None, **options) (class) [#]

A text entry field.

Parent widget.
Widget options. See the description of the config method for a list of available options.

config(**options) [#]

Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.

Widget options.
Widget background. The default is system specific. (the option database name is background, the class is Background)
Same as background.
Border width. The default is system specific, but is usually a few pixels. (borderWidth/BorderWidth)
Same as borderwidth.
Widget cursor. The default is a text insertion cursor (typically an “I-beam” cursor, e.g. xterm). (cursor/Cursor)
Background to use when the widget is disabled. If omitted or blank, the standard background is used instead. (disabledBackground/DisabledBackground)
Text color to use when the widget is disabled. If omitted or blank, the standard foreground is used instead. (disabledForeground/DisabledForeground)
If true, selected text is automatically exported to the clipboard. Default is true. (exportSelection/ExportSelection)
Widget font. The default is system specific. (font/Font)
Text color. (foreground/Foreground)
Same as foreground.
Together with highlightcolor, this option controls how to draw the focus highlight border. This option is used when the widget doesn’t have focus. The default is system specific. (highlightBackground/HighlightBackground)
Same as highlightbackground, but is used when the widget has focus. (highlightColor/HighlightColor)
The width of the focus highlight border. Default is typically a few pixels, unless the system indicates focus by modifying the button itself (like on Windows). (highlightThickness/HighlightThickness)
Color used for the insertion cursor. (insertBackground/Foreground)
Width of the insertion cursor’s border. If this is set to a non-zero value, the cursor is drawn using the RAISED border style. (insertBorderWidth/BorderWidth)
Together with insertontime, this option controls cursor blinking. Both values are given in milliseconds. (insertOffTime/OffTime)
See insertofftime. (insertOnTime/OnTime)
Width of the insertion cursor. Usually one or two pixels. (insertWidth/InsertWidth)
FIXME. No default. (invalidCommand/InvalidCommand)
Same as invalidcommand.
How to align the text inside the entry field. Use one of LEFT, CENTER, or RIGHT. The default is LEFT. (justify/Justify)
The background color to use when the state is “readonly”. If omitted or blank, the standard background is used instead. (readonlyBackground/ReadonlyBackground)
Border style. The default is SUNKEN. Other possible values are FLAT, RAISED, GROOVE, and RIDGE. (relief/Relief)
Selection background color. The default is system and display specific. (selectBackground/Foreground)
Selection border width. The default is system specific. (selectBorderWidth/BorderWidth)te
Selection text color. The default is system specific. (selectForeground/Background)
Controls how to display the contents of the widget. If non-empty, the widget displays a string of characters instead of the actual contents. To get a password entry widget, set this option to “*”. (show/Show)
The entry state: NORMAL, DISABLED, or “readonly” (same as DISABLED, but contents can still be selected and copied). Default is NORMAL. Note that if you set this to DISABLED or “readonly”, calls to insert and delete are ignored. (state/State)
Indicates that the user can use the Tab key to move to this widget. Default is an empty string, which means that the entry widget accepts focus only if it has any keyboard bindings (default is on, in other words). (takeFocus/TakeFocus)
Associates a tkinter variable (usually a StringVar) to the contents of the entry field. (textVariable/Variable)
Specifies when validation should be done. You can use “focus” to validate whenever the widget gets or loses the focus, “focusin” to validate only when it gets focus, “focusout” to validate when it loses focus, “key” on any modification, and ALL for all situations. Default is NONE (no validation). (validate/Validate)
A function or method to call to check if the contents is valid. The function should return a true value if the new contents is valid, or false if it isn’t. Note that this option is only used if the validate option is not NONE. (validateCommand/ValidateCommand)
Same as validatecommand.
Width of the entry field, in character units. Note that this controlS the size on screen; it does not limit the number of characters that can be typed into the entry field. The default width is 20 character. (width/Width)
Used to connect an entry field to a horizontal scrollbar. This option should be set to the set method of the corresponding scrollbar. (xScrollCommand/ScrollCommand)

delete(first, last=None) [#]

Deletes the character at index, or within the given range. Use delete(0, END) to delete all text in the widget.

Start of range.
Optional end of range. If omitted, only a single character is removed.

get() [#]

Gets the current contents of the entry field.

The widget contents, as a string.

icursor(index) [#]

Moves the insertion cursor to the given index. This also sets the INSERT index.

Where to move the cursor.

index(index) [#]

Gets the numerical position corresponding to the given index.

An index.
The corresponding numerical index.

insert(index, string) [#]

Inserts text at the given index. Use insert(INSERT, text) to insert text at the cursor, insert(END, text) to append text to the widget.

Where to insert the text.
The text to insert.

scan_dragto(x) [#]

Sets the scanning anchor for fast horizontal scrolling to the given mouse coordinate.

Current horizontal mouse position.

scan_mark(x) [#]

Scrolls the widget contents sideways according to the given mouse coordinate. The text is moved 10 times the distance between the scanning anchor and the new position.

Current horizontal mouse position.

select_adjust(index) [#]

Same as selection_adjust.

select_clear() [#]

Same as selection_clear.

select_from(index) [#]

Same as selection_from.

select_present() [#]

Same as selection_present.

select_range(start, end) [#]

Same as selection_range.

select_to(index) [#]

Same as selection_to.

selection_adjust(index) [#]

Adjusts the selection to include also the given character. If index is already selected, do nothing.

The index.

selection_clear() [#]

Clears the selection.

selection_from(index) [#]

Starts a new selection. This also sets the ANCHOR index.

The index.

selection_present() [#]

Checks if text is selected.

A true value if some part of the text is selected.

selection_range(start, end) [#]

Explicitly sets the selection range. Start must be smaller than end. Use selection_range(0, END) to select all text in the widget.

Start of selection range.
End of range.

selection_to(index) [#]

Selects all text between ANCHOR and the given index.


xview(index) [#]

Makes sure the given index is visible. The entry view is scrolled if necessary.

An index.

xview_moveto(fraction) [#]

Adjusts the entry view so that the given offset is at the left edge of the canvas. Offset 0.0 is the beginning of the entry string, 1.0 the end.


xview_scroll(number, what) [#]

Scrolls the entry view horizontally by the given amount.

Number of units.
What unit to use. This can be either “units” (characters) or “pages” (larger steps).


A Django site. rendered by a django application. hosted by webfaction.