Back to: Python Programming
0
The Entry Widget
The Entry Widget is a Tkinter Widget used to Enter or display a single line of text.
Syntax : entry = tk.Entry(parent, options)
Parameters:
- Parent: The Parent window or frame in which the widget to display.
- Options: The various options provided by the entry widget are:
- bg : The normal background colour displayed behind the label and indicator.
- bd : The size of the border around the indicator. Default is 2 pixels.
- font : The font used for the text.
- fg : The colour used to render the text.
- justify : If the text contains multiple lines, this option controls how the text is justified: CENTER, LEFT, or RIGHT.
- relief : With the default value, relief=FLAT. You may set this option to any of the other styles like : SUNKEN, RIGID, RAISED, GROOVE
- show : Normally, the characters that the user types appear in the entry. To make a .password. entry that echoes each character as an asterisk, set show=”*”.
- textvariable : In 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.
Methods: The various methods provided by the entry widget are:
- insert ( index, ‘name’) : Inserts string ‘name’ before the character at the given index.
- get() : Returns the entry’s current text as a string.
- delete() : Deletes characters from the widget
To demonstrate the entry widget, it is easiest to write some code and describe what is happening on each line.
Lets write some code to display a username – It’ll look like this:

# Entry Widget - a textbox that accepts a single line of user input
from tkinter import *
#~~ create a function to get user input and output to screen ~~
def submit():
username = entry.get() # <-- gets entry text
print("Hello "+username) # <-- could use an f-string here
#~~ create a function to delete ALL user input from Entry Widget ~~
def delete():
entry.delete(0,END) # <--deletes/clears the line of text
#~~ create a function to remove characters from the right ~~
def backspace():
entry.delete(len(entry.get())-1,END) # <-- deletes the last character
#~~ create a window with a TITLE and background colour ~~
window = Tk()
window.title("User input banner")
window.config(background="#caa7e6")
#~~ create a LABEL for the word "username:" on the left ~~
label = Label(window, text="username: ",
bg='#caa7e6',
font=("Consolas",30))
#~~ create a BUTTON to call the SUBMIT function ~~
submit = Button(window, text="submit", command=submit)
#~~ create a BUTTON to call the DELETE function ~~
delete = Button(window, text="delete", command=delete)
#~~ create a BUTTON to call the BACKSPACE function ~~
backspace = Button(window, text="backspace", command=backspace)
# ~~ create the ENTRY part where a user enters a string ~~
entry = Entry(window, font=('Ink Free',50),
bg='#111111',
fg='#00FF00',
width=10) # <-- width displayed in characters
#entry.insert(0,'Spongebob') # <-- default text, 0 is a positional argument
#entry.config(state=DISABLED) # <-- set to ACTIVE/DISABLED
#entry.config(show='*') # <-- replace characters shown with * character
#~~ place each WIDGET on the screen in the required order ~~
label.pack(side=LEFT)
entry.pack(side=LEFT)
submit.pack(side = RIGHT)
delete.pack(side = RIGHT)
backspace.pack(side = RIGHT)
#~~ performing an infinite loop for the window to display ~~
window.mainloop()
The next example creates a simple login screen:

# ~~ Program to make a simple login screen ~~
import tkinter as tk
# ~~ create window & set the window size & title ~~
window = tk.Tk()
window.geometry("400x200")
window.title("Login Demo")
#~~ declare string variables for storing name and password ~~
name_var = tk.StringVar()
passw_var = tk.StringVar()
#~~ defining function to get name and password and print on screen ~~
def submit():
name = name_var.get()
password = passw_var.get()
print("The username is : " + name)
print("The password is : " + password)
name_var.set("")
passw_var.set("")
#~~ create a LABEL for name using widget Label ~~
name_label = tk.Label(window,
text='Username',
font=('calibre', 10, 'bold'))
#~~ creating an ENTRY for input name using widget Entry ~~
name_entry = tk.Entry(window, textvariable=name_var,
font=('calibre', 10, 'normal'))
#~~ creating a LABEL for password ~~
passw_label = tk.Label(window, text='Password',
font=('calibre', 10, 'bold'))
#~~ creating an ENTRY for password ~~
passw_entry = tk.Entry(window, textvariable=passw_var,
font=('calibre', 10, 'normal'),
show='*')
#~~ Button that will call the submit function ~~
sub_btn = tk.Button(window, text='Submit',
command=submit)
#~~ placing label & entry in position using grid method ~~
name_label.grid(row=0, column=0)
name_entry.grid(row=0, column=1)
passw_label.grid(row=1, column=0)
passw_entry.grid(row=1, column=1)
sub_btn.grid(row=2, column=1)
#~~ performing an infinite loop for the window to display~~
window.mainloop()