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 Name | Description |
---|---|
background / bg | Sets the background color of widget, Default Is platform specific |
borderwidth / bd | Width of entry widgets border. The default is system specific but usually 1 to 2 pixels. |
cursor | The cursor to display when the mouse moves over the widget. By default, the standard cursor is used. |
disabledforeground | The foreground color to use when the wiget is disabled or invalid. The default is system specific. |
exportselection | If true, the selected text is automatically exported to the clipboard. The default is true. |
font | Font to use for widget. The default is system specific. |
foreground / fg | The foreground color for this widget. Default is system specific |
justify | Align the text inside the entry field. Use one of LEFT, CENTER, or RIGHT. The default is LEFT. |
textvariable | Associates the Tkinter variable (usually StringVar) with the contents of the entry widget. |
padx | Horizontal padding to add around the text. The default is 1 pixel. |
pady | Vertical padding to add around the text. The default is 1 pixel. |