The Entry widget in Tkinter is used to create a single-line text box that allows users to input text. It is suitable for scenarios where only a single line of text input is needed. For multi-line text, the Text widget should be used.
w = Entry(parent, options)
Here, parent refers to the container widget (such as a window or frame), and options are used to configure the appearance and behavior of the Entry widget.
| SN | Option | Description |
|---|---|---|
| 1 | bg | Background color of the widget. |
| 2 | bd | Border width of the widget in pixels. |
| 3 | cursor | Changes the cursor type when over the Entry widget (e.g., arrow, dot). |
| 4 | exportselection | Determines if the text is automatically copied to the clipboard. Set to 0 to prevent copying. |
| 5 | fg | Foreground color (text color) of the Entry widget. |
| 6 | font | Font type used for the text in the Entry widget. |
| 7 | highlightbackground | Color of the highlight when the widget does not have focus. |
| 8 | highlightcolor | Color of the focus highlight rectangle around the widget. |
| 9 | highlightthickness | Width of the highlight rectangle around the widget when it has focus. |
| 10 | insertbackground | Color of the insertion cursor (caret). |
| 11 | insertborderwidth | Width of the 3-D border around the insertion cursor. |
| 12 | insertofftime | Duration (in milliseconds) that the insertion cursor remains off during blinking. |
| 13 | insertontime | Duration (in milliseconds) that the insertion cursor remains on during blinking. |
| 14 | insertwidth | Width of the insertion cursor. |
| 15 | justify | Alignment of the text if multiple lines are present (though Entry typically supports single line text). |
| 16 | relief | Type of the border around the widget (FLAT, RAISED, SUNKEN, GROOVE, RIDGE). Default is FLAT. |
| 17 | selectbackground | Background color of selected text. |
| 18 | selectborderwidth | Width of the border around selected text. |
| 19 | selectforeground | Font color of selected text. |
| 20 | show | Used to display text as a different character (e.g., * for passwords). |
| 21 | textvariable | A StringVar instance used to retrieve the text from the Entry widget. |
| 22 | width | Width of the Entry widget in characters. |
| 23 | xscrollcommand | Links the Entry widget to a horizontal scrollbar if needed. |
| SN | Method | Description |
|---|---|---|
| 1 | delete(first, last=None) | Deletes text from the specified range (first to last). |
| 2 | get() | Retrieves the text currently in the Entry widget. |
| 3 | icursor(index) | Sets the insertion cursor position to the specified index. |
| 4 | index(index) | Returns the index position of the specified character. |
| 5 | insert(index, s) | Inserts the specified string s before the character at the specified index. |
| 6 | select_adjust(index) | Adjusts the selection to include the character at the specified index. |
| 7 | select_clear() | Clears the current text selection. |
| 8 | select_from(index) | Sets the start index for text selection to the specified index. |
| 9 | select_present() | Returns True if any text is selected; otherwise False. |
| 10 | select_range(start, end) | Selects text in the range from start to end. |
| 11 | select_to(index) | Selects text from the start to the specified index. |
| 12 | xview(index) | Links the Entry widget to a horizontal scrollbar, setting its view to the specified index. |
| 13 | xview_scroll(number, what) | Scrolls the view of the Entry widget horizontally by number units, depending on what (UNITS or PAGES). |
Here is an example of a basic calculator using the Entry widget:
from tkinter import *
def click(event):
# Get the value from the entry widget
entry_text = entry.get()
# Evaluate the expression
try:
result = eval(entry_text)
entry.delete(0, END)
entry.insert(0, str(result))
except Exception as e:
entry.delete(0, END)
entry.insert(0, "Error")
# Create the main window
root = Tk()
root.title("Simple Calculator")
# Create an entry widget for the input
entry = Entry(root, width=16, font=('Arial', 24), borderwidth=2, relief='solid')
entry.grid(row=0, column=0, columnspan=4)
# Create buttons
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]
row_val = 1
col_val = 0
for button in buttons:
Button(root, text=button, width=4, height=2, font=('Arial', 18),
command=lambda b=button: entry.insert(END, b) if b != '=' else click(None)).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# Run the application
root.mainloop()
• Entry Widget: Used to display and input text. It's where the user enters mathematical expressions.
• Buttons: Create a grid of buttons for digits and operators. Clicking a button inserts its value into the Entry widget. Clicking "=" evaluates the expression.
• Function: click() evaluates the expression entered in the Entry widget and displays the result.
This example demonstrates how to use the Entry widget in combination with buttons to create a simple calculator application.