Next / Previous / Contents / TCC Help System / NM Tech homepage

8. The Entry widget

The purpose of an Entry widget is to let the user see and modify a single line of text.

Some definitions:

To create a new Entry widget in a root window or frame named master:

    w = Entry ( master, option, ... )

This constructor returns the entry widget object. Options include:

bg or backgroundThe background color inside the entry area. Default is a light gray.
bd or borderwidthThe width of the border around the entry area. Default is 2.
cursorThe cursor used when the mouse is within the entry widget; see Section 4.8, “Cursors”.
fontThe font used for text entered in the widget by the user. See Section 4.4, “Type fonts”.
exportselectionBy default, if you select text within an Entry widget, it is automatically exported to the clipboard. To avoid this exportation, use exportselection=0.
fg or foregroundThe color used to render the text. Default is black.
highlightbackgroundColor of the focus highlight when the widget does not have focus. See Section 23, “Focus: routing keyboard input”.
highlightcolorColor shown in the focus highlight when the widget has the focus.
highlightthicknessThickness of the focus highlight.
insertbackgroundBy default, the insertion cursor (which shows the point within the text where new keyboard input will be inserted) is black. To get a different color of insertion cursor, set insertbackground to any color; see Section 4.3, “Colors”.
insertborderwidthBy default, the insertion cursor is a simple rectangle. You can get the cursor with the RAISED relief effect (see Section 4.6, “Relief styles”) by setting insertborderwidth to the dimension of the 3-d border. If you do, make sure that the insertwidth attribute is at least twice that value.
insertofftimeBy default, the insertion cursor blinks. You can set insertofftime to a value in milliseconds to specify how much time the insertion cursor spends off. Default is 300. If you use insertofftime=0, the insertion cursor won't blink at all.
insertontimeSimilar to insertofftime, this attribute specifies how much time the cursor spends on per blink. Default is 600 (milliseconds).
insertwidthBy default, the insertion cursor is 2 pixels wide. You can adjust this by setting insertwidth to any dimension.
justifyThis option controls how the text is justified when the text doesn't fill the widget's width. The value can be LEFT (the default), CENTER, or RIGHT.
reliefSelects three-dimensional shading effects around the text entry. See Section 4.6, “Relief styles”. The default is relief=SUNKEN.
selectbackgroundThe background color to use displaying selected text. See Section 4.3, “Colors”.
selectborderwidthThe width of the border to use around selected text. The default is one pixel.
selectforegroundThe foreground (text) color of selected text.
showNormally, the characters that the user types appear in the entry. To make a “password” entry that echoes each character as an asterisk, set show="*".
stateUse this attribute to disable the Entry widget so that the user can't type anything into it. Use state=DISABLED to disable the widget, state=NORMAL to allow user input again. Your program can also find out whether the cursor is currently over the widget by interrogating this attribute; it will have the value ACTIVE when the mouse is over it.
takefocusBy default, the focus will tab through entry widgets. Set this option to 0 to take the widget out of the sequence. For a discussion of focus, see Section 23, “Focus: routing keyboard input”.
textvariableIn order to be able to retrieve the current text from your entry widget, you must set this option to an instance of the StringVar class; see Section 22, “Control variables: the values behind the widgets”. You can retrieve the text using v.get(), or set it using v.set(), where v is the associated control variable.
widthThe size of the entry in characters. The default is 20. For proportional fonts, the physical length of the widget will be based on the average width of a character times the value of the width option.
xscrollcommandIf you expect that users will often enter more text than the onscreen size of the widget, you can link your entry widget to a scrollbar. Set this option to the .set method of the scrollbar. For more information, see Section 8.1, “Scrolling an Entry widget”.

Methods on Entry objects include:

.delete ( first, last=None )

Deletes characters from the widget, starting with the one at index first, up to but not including the character at position last. If the second argument is omitted, only the single character at position first is deleted.

.get()

Returns the entry's current text as a string.

.icursor ( index )

Set the insertion cursor just before the character at the given index.

.index ( index )

Shift the contents of the entry so that the character at the given index is the leftmost visible character. Has no effect if the text fits entirely within the entry.

.insert ( index, s )

Inserts string s before the character at the given index.

.select_adjust ( index )

This method is used to make sure that the selection includes the character at the specified index. If the selection already includes that character, nothing happens. If not, the selection is expanded from its current position (if any) to include position index.

.select_clear()

Clears the selection. If there isn't currently a selection, has no effect.

.select_from ( index )

Sets the ANCHOR index position to the character selected by index, and selects that character.

.select_present()

If there is a selection, returns true, else returns false.

.select_range ( start, end )

Sets the selection under program control. Selects the text starting at the start index, up to but not including the character at the end index. The start position must be before the end position.

To select all the text in an entry widget e, use e.select_range(0, END).

.select_to ( index )

Selects all the text from the ANCHOR position up to but not including the character at the given index.

.xview ( index )

Same as .xview(). This method is useful in linking the Entry widget to a horizontal scrollbar. See Section 8.1, “Scrolling an Entry widget”.

.xview_moveto ( f )

Positions the text in the entry so that the character at position f, relative to the entire text, is positioned at the left edge of the window. The f argument must be in the range [0,1], where 0 means the left end of the text and 1 the right end.

.xview_scroll ( number, what )

Used to scroll the entry horizontally. The what argument must be either UNITS, to scroll by character widths, or PAGES, to scroll by chunks the size of the entry widget. The number is positive to scroll left to right, negative to scroll right to left. For example, for an entry widget e, e.xview_scroll(-1, PAGES) would move the text one “page” to the right, and e.xview_scroll(4, UNITS) would move the text four characters to the left.