In this short guide, you’ll see how to create an entry box using tkinter.
More specifically, you’ll observe 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
To start, here is the syntax to create the entry box (in the next section, you’ll see the full steps to derive this code, including a more stylish version of the entry box):
import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=400, height=300) canvas.pack() entry = tk.Entry(root) canvas.create_window(200, 140, window=entry) def get_square_root(): value = entry.get() label = tk.Label(root, text=float(value) ** 0.5) canvas.create_window(200, 230, window=label) button = tk.Button(text="Get the Square Root", command=get_square_root) canvas.create_window(200, 180, window=button) root.mainloop()
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:
canvas = tk.Canvas(root, width=400, height=300) canvas.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):
entry = tk.Entry(root) canvas.create_window(200, 140, window=entry)
Step 3: Include a function
The get_square_root function will be used to get the value that was typed in the entry box, and then derive the square root. The square root calculation is captured in the label:
text=float(value) ** 0.5
So the full get_square_root function would look like this:
def get_square_root(): value = entry.get() label = tk.Label(root, text=float(value) ** 0.5) canvas.create_window(200, 230, window=label)
Step 4: Add a button
The button can be used to execute the get_square_root function:
button = tk.Button(text="Get the Square Root", command=get_square_root) canvas.create_window(200, 180, window=button)
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() canvas = tk.Canvas(root, width=400, height=300) canvas.pack() entry = tk.Entry(root) canvas.create_window(200, 140, window=entry) def get_square_root(): value = entry.get() label = tk.Label(root, text=float(value) ** 0.5) canvas.create_window(200, 230, window=label) button = tk.Button(text="Get the Square Root", command=get_square_root) canvas.create_window(200, 180, window=button) root.mainloop()
Once you run the code in Python, you’ll see the display with the 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. You’ll then get the result of 5, which is indeed the square root of 25.
Styling the tkinter entry box
You can further style the tkinter entry box 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() canvas = tk.Canvas(root, width=400, height=300, relief="raised") canvas.pack() label1 = tk.Label(root, text="Calculate the Square Root") label1.config(font=("arial", 14)) canvas.create_window(200, 25, window=label1) label2 = tk.Label(root, text="Type your Number:") label2.config(font=("arial", 10)) canvas.create_window(200, 100, window=label2) entry = tk.Entry(root) canvas.create_window(200, 140, window=entry) def get_square_root(): value = entry.get() label3 = tk.Label( root, text="The Square Root of " + value + " is:", font=("arial", 10) ) canvas.create_window(200, 210, window=label3) label4 = tk.Label(root, text=float(value) ** 0.5, font=("arial", 10, "bold")) canvas.create_window(200, 230, window=label4) button = tk.Button( text="Get the Square Root", command=get_square_root, bg="brown", fg="white", font=("arial", 9, "bold"), ) canvas.create_window(200, 180, window=button) root.mainloop()
Run the code in Python, and you’ll get the styled tkinter display.