What is Entry Widget?

Entry widget is a standard Tkinter widget to take single line input from the user. It can also be used to show some editable information to the user.

Example

The following Python script creates a window containing one entry and a button widget. Entry widget is used here to take the user’s name and on buttons click entered name will be printed on the console.

You need to write following code in any text editor and then save it with .py extension. To execute the application type py EntryExample.py command in terminal.

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

# Entry widget
entry = Entry(window)
entry.pack()

# Python function associated with 'button'
def sayHello():
    name = entry.get()
    print("Hello", name)

# A button
button = Button(window, text="Say, Hello", command=sayHello)
button.pack()

window.mainloop()

Output

Explanation

Following line creates a Tkinter window

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

See explanation of Tkinter window – Tkinter top level window


The following block of code creates an entry widget.

# Entry widget
entry = Entry(window)
entry.pack()

Properties

  • window – The container window.

The second line entry.pack(), is geometry manager that packs widgets into rows or columns.


This block creates a button widget

# A button
button = Button(window, text="Say, Hello", command=sayHello)
button.pack()

Here button widget is used to print user’s entered text into the console.
See more about button widget – Tkinter button widget

Some entry properties

Property NameDescription
background / bgSets the background color of widget, Default Is platform specific
borderwidth / bdWidth of entry widgets border. The default is system specific but usually 1 to 2 pixels.
cursorThe cursor to display when the mouse moves over the widget. By default, the standard cursor is used.
disabledforegroundThe foreground color to use when the wiget is disabled or invalid. The default is system specific.
exportselectionIf true, the selected text is automatically exported to the clipboard. The default is true.
fontFont to use for widget. The default is system specific.
foreground / fgThe foreground color for this widget. Default is system specific
justifyAlign the text inside the entry field. Use one of LEFT, CENTER, or RIGHT. The default is LEFT.
textvariableAssociates the Tkinter variable (usually StringVar) with the contents of the entry widget.
padxHorizontal padding to add around the text. The default is 1 pixel.
padyVertical padding to add around the text. The default is 1 pixel.