Python Tkinter Entry

The Entry widget is used to provde the single line text-box to the user to accept a value from the user. We can use the Entry widget to accept the text strings from the user. It can only be used for one line of text from the user. For multiple lines of text, we must use the text widget.

The syntax to use the Entry widget is given below.

Syntax

A list of possible options is given below.

SNOptionDescription
1bgThe background color of the widget.
2bdThe border width of the widget in pixels.
3cursorThe mouse pointer will be changed to the cursor type set to the arrow, dot, etc.
4exportselectionThe text written inside the entry box will be automatically copied to the clipboard by default. We can set the exportselection to 0 to not copy this.
5fgIt represents the color of the text.
6fontIt represents the font type of the text.
7highlightbackgroundIt represents the color to display in the traversal highlight region when the widget does not have the input focus.
8highlightcolorIt represents the color to use for the traversal highlight rectangle that is drawn around the widget when it has the input focus.
9highlightthicknessIt represents a non-negative value indicating the width of the highlight rectangle to draw around the outside of the widget when it has the input focus.
10insertbackgroundIt represents the color to use as background in the area covered by the insertion cursor. This color will normally override either the normal background for the widget.
11insertborderwidthIt represents a non-negative value indicating the width of the 3-D border to draw around the insertion cursor. The value may have any of the forms acceptable to Tk_GetPixels.
12insertofftimeIt represents a non-negative integer value indicating the number of milliseconds the insertion cursor should remain "off" in each blink cycle. If this option is zero, then the cursor doesn't blink: it is on all the time.
13insertontimeSpecifies a non-negative integer value indicating the number of milliseconds the insertion cursor should remain "on" in each blink cycle.
14insertwidthIt represents the value indicating the total width of the insertion cursor. The value may have any of the forms acceptable to Tk_GetPixels.
15justifyIt specifies how the text is organized if the text contains multiple lines.
16reliefIt specifies the type of the border. Its default value is FLAT.
17selectbackgroundThe background color of the selected text.
18selectborderwidthThe width of the border to display around the selected task.
19selectforegroundThe font color of the selected task.
20showIt is used to show the entry text of some other type instead of the string. For example, the password is typed using stars (*).
21textvariableIt is set to the instance of the StringVar to retrieve the text from the entry.
22widthThe width of the displayed text or image.
23xscrollcommandThe entry widget can be linked to the horizontal scrollbar if we want the user to enter more text then the actual width of the widget.

Example

Output:

Python Tkinter Entry

Entry widget methods

Python provides various methods to configure the data written inside the widget. There are the following methods provided by the Entry widget.

SNMethodDescription
1delete(first, last = none)It is used to delete the specified characters inside the widget.
2get()It is used to get the text written inside the widget.
3icursor(index)It is used to change the insertion cursor position. We can specify the index of the character before which, the cursor to be placed.
4index(index)It is used to place the cursor to the left of the character written at the specified index.
5insert(index,s)It is used to insert the specified string before the character placed at the specified index.
6select_adjust(index)It includes the selection of the character present at the specified index.
7select_clear()It clears the selection if some selection has been done.
8select_form(index)It sets the anchor index position to the character specified by the index.
9select_present()It returns true if some text in the Entry is selected otherwise returns false.
10select_range(start,end)It selects the characters to exist between the specified range.
11select_to(index)It selects all the characters from the beginning to the specified index.
12xview(index)It is used to link the entry widget to a horizontal scrollbar.
13xview_scroll(number,what)It is used to make the entry scrollable horizontally.

Example: A simple calculator

Output:

Python Tkinter Entry



Latest Courses