Tkinter Entry

In this tutorial, I will teach you about the Tkinter entry widget in python. for a better understanding of concepts, I will also be giving you practical examples. so let’s get started.

Entry is the standard widget in Tkinter that allows the users to enter a single line of text inside GUI applications. we can later access that text inside our code and process according to our requirements.

Syntax :

e = Entry(master , **options)

Options :

Here is the table of various options along with descriptions that we can use to customize the behavior of the entry widget.

backgroundUse this option to set the background color of the entry.
borderwidthSet the width of the border of the entry widget.
cursorSet the text insertion cursor of the entry.
disabledbackgroundSet the background color of disabled entry.
disabledforegroundSet the text color of disabled entry.
exportselectionExport the selected text to clipboard default is True.
fontSet the font for the entry text.
foregroundSet the text color of the entry.
highlightbackgroundSet the background color of entry. when on focus
highlightthicknessSet the border width of entry. when on focus.
insertbackgroundSet the color of the text insertion cursor.
insertborderwidthSet the width of the text insertion cursor.
justifySet the position for the entry content. LEFT(default), RIGHT, CENTER.
reliefSet the border style. SUNKEN(default).FLATRAISEDGROOVE, RIDGE.
showUse this option to control how to show the contents of the entry.
stateSet the state of the entry. NORMAL(default), DISABLED,”readonly”.
textvariableUse this option to control the text of the entry.
widthSet the width of the entry.

Tkinter Entry Examples

Lets look at some practical examples for the tkinter entry.

Note: To able to understand these examples I assume you have some basic knowledge of Tkinter label and button.

Example 1 : Simple Tkinter Entry

In this example, we will create a basic entry and access its content on a button click.

Here is the code for that.

from tkinter import *

window = Tk()

# Create the entry inside window.
entry = Entry(window)

def get_text():
    # Function to print entry text.
    print("Text : ",entry.get())

# Create a button with text 'Get'.
# On click : print the entry text on console.
button = Button(window,text = "Get",command = get_text)

entry.pack()
button.pack()

window.mainloop()

Save and run the above code. you should see the following output.

tkinter entry example 1 output

Example 2 : Login Form

In this example, We will be using ttk widgets to create a simple login form with username and password field. we will also check and validate the form data.

from tkinter import *
from tkinter import ttk

# In this example we are using ttk widgets
# They have better appearence than default widgets.

window = Tk()
window.title("Login App")

# Instead of using get() method on entry
# we are using StringVar
USERNAME = StringVar()
PASSWORD = StringVar()


def check_data():
    # function to check username and password
    if USERNAME.get() == 'john' and PASSWORD.get() == 'testing':
        print("Logged In Successfully")
    else:
        print("Check your Username/Password")

def clear():
    # Function to clear entry fields
    USERNAME.set("")
    PASSWORD.set("")


# Create Labels
ttk.Label(window,text = "Username : ").grid(
    row = 0 , column = 0,padx = 10 , pady = 10)
ttk.Label(window,text = "Password : ").grid(
    row = 1 , column = 0,padx = 10 , pady = 10)


# Create Entries
# Control the text using textvariable
ttk.Entry(window,textvariable = USERNAME).grid(
    row = 0 , column = 1,padx = 10 , pady = 10)

# Here we are using the show option to hide actual input
ttk.Entry(window,textvariable = PASSWORD,show = "*").grid(
    row = 1 , column = 1,padx = 10 , pady = 10)

# Create Buttons
ttk.Button(window,text = "Clear",command = clear).grid(
    row = 2 , column = 0,padx = 10 , pady = 10)
ttk.Button(window,text = "Login",command = check_data).grid(
    row = 2 , column = 1,padx = 10 , pady = 10)

window.mainloop()

Output :

tkinter entry example 2 output

Example 3 : Simple Calculator

In this final example, we will be using the above-discussed styling options to create a simple calculator.

The purpose of this example is to show how the widget options can be helpful in real-life projects.

from tkinter import *

window = Tk()
window.title("Calculator") # set the title
window.geometry("500x175") # set the dimensions of window
window['background']='black' # set the background color of window


RESULT = StringVar() # Variable to control result text
NUM1 = StringVar() # Variable to control num1 text
NUM2 = StringVar() # Variable to control num2 text


def add(): # Function to perform add
    r = float(NUM1.get()) + float(NUM2.get()) # Add two nums
    RESULT.set(str(r)) # Set the result

def subtract():
    r = float(NUM1.get()) - float(NUM2.get())
    RESULT.set(str(r))

def multiply():
    r = float(NUM1.get()) * float(NUM2.get())
    RESULT.set(str(r))

def divide():
    r = float(NUM1.get()) / float(NUM2.get())
    RESULT.set(str(r))

# Here we are using a trick to apply styles to widgets
LABEL_STYLES = {"padx":10,"pady":10,"font":"verdana 13 bold","fg":"white","bg":"black"}
ENTRY_STYLES = {"font":"verdana 15 bold","bd":0,"width":20,"justify":CENTER}
BUTTON_STYLES = {"font":"verdana 13 bold","bd":0}

# Create Labels
Label(window,text = "Enter Number 1 : ",
    **LABEL_STYLES).grid(row = 0 , column = 0)
Label(window,text = "Enter Number 2 : ",
    **LABEL_STYLES).grid(row = 1 , column = 0)
Label(window,text = "Result Value   : ",
    **LABEL_STYLES).grid(row = 2 , column = 0)

# Create Entries
Entry(window,textvariable = NUM1,
    **ENTRY_STYLES).grid(row = 0 , column = 1,columnspan = 4)
Entry(window,textvariable = NUM2,
    **ENTRY_STYLES).grid(row = 1 , column = 1,columnspan = 4)

# For this result entry we are setting its state to readonly
Entry(window,textvariable = RESULT,state = "readonly"
    ,**ENTRY_STYLES).grid(row = 2 , column = 1,columnspan = 4)


# Create Buttons
Button(window,text = "Add",command = add
    ,**BUTTON_STYLES).grid(row = 3 , column = 1)
Button(window,text = "Sub",command = subtract,
    **BUTTON_STYLES).grid(row = 3 , column = 2)
Button(window,text = "Mul",command = multiply,
    **BUTTON_STYLES).grid(row = 3 , column = 3)
Button(window,text = "Div",command = divide,
    **BUTTON_STYLES).grid(row = 3 , column = 4)

window.mainloop()

Output :

tkinter entry example 3 output

That’s the wrap for this tutorial.

Leave a Reply