How to Create an Entry Box using Tkinter

In this tutorial, I’ll show you how to create an entry box using tkinter.

More specifically, I’ll review a simple example with the steps to create:

  • An entry box which can be used to get the user’s input
  • Calculate the square root based on the input collected

By the end of this tutorial, you would be able to create the following entry box using tkinter:

How to Create an Entry Box using Tkinter

Steps to Create an Entry Box using Tkinter

Step 1: Create the Canvas

The Canvas is your display where you can place items, such as entry boxes, buttons, charts and more. You can control the dimensions of your Canvas by changing the width and height values:

canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()

Step 2: Add the entry box

An entry box can be used to get the user’s input. You can specify the position where the entry box would be placed on your Canvas (currently the position is set to 200, 140):

entry1 = tk.Entry (root) 
canvas1.create_window(200, 140, window=entry1)

Step 3: Include a function

The getSquareRoot function will be used to get the value that was typed in the entry box, and then use that value to derive the square root. The square root calculation is captured in the label:

text= float(x1)**0.5

So the full getSquareRoot function would look like this:

def getSquareRoot ():  
    x1 = entry1.get()
    
    label1 = tk.Label(root, text= float(x1)**0.5)
    canvas1.create_window(200, 230, window=label1)

Step 4: Add a button

The button can be used to execute the getSquareRoot function:

button1 = tk.Button(text='Get the Square Root', command=getSquareRoot)
canvas1.create_window(200, 180, window=button1)

Step 5: Run the complete code in Python

This is how the complete code would look like after putting all the above components together:

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()

entry1 = tk.Entry (root) 
canvas1.create_window(200, 140, window=entry1)

def getSquareRoot ():  
    x1 = entry1.get()
    
    label1 = tk.Label(root, text= float(x1)**0.5)
    canvas1.create_window(200, 230, window=label1)
    
button1 = tk.Button(text='Get the Square Root', command=getSquareRoot)
canvas1.create_window(200, 180, window=button1)

root.mainloop()

Once you run the code, you’ll see the following display with the entry box:

tkinter entry box

You can test that the code is working by typing a value within the entry box. For example, type the value of 25 within the box, and then click on the Get the Square Root button:

Create an Entry Box using Tkinter

You would then get the result of 5, which is indeed the square root of 25:

Entry Box using Tkinter

Styling the tkinter display

You can further style the tkinter display by using the code below.

Feel free to change the labels, fonts and/or colors based on your needs.

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 400, height = 300,  relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='Calculate the Square Root')
label1.config(font=('helvetica', 14))
canvas1.create_window(200, 25, window=label1)

label2 = tk.Label(root, text='Type your Number:')
label2.config(font=('helvetica', 10))
canvas1.create_window(200, 100, window=label2)

entry1 = tk.Entry (root) 
canvas1.create_window(200, 140, window=entry1)

def getSquareRoot ():
    
    x1 = entry1.get()
    
    label3 = tk.Label(root, text= 'The Square Root of ' + x1 + ' is:',font=('helvetica', 10))
    canvas1.create_window(200, 210, window=label3)
    
    label4 = tk.Label(root, text= float(x1)**0.5,font=('helvetica', 10, 'bold'))
    canvas1.create_window(200, 230, window=label4)
    
button1 = tk.Button(text='Get the Square Root', command=getSquareRoot, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 180, window=button1)

root.mainloop()

Run the code in Python, and you would get the following tkinter display:

Box in Python

Using the same example as before, type the value of 25 within the entry box, and then click on the Get the Square Root button:

Example of Entry Box in Python

You would then get the result of 5 as expected:

tkinter