Beginner’s Guide to Creating a Tkinter Data Entry Form in Python – Responsive GUI Project Tutorial

Posted by


In this tutorial, we will be creating a simple data entry form using Tkinter in Python, which will have a responsive layout for user-friendly navigation and input.

Tkinter is a standard GUI toolkit for Python, providing easy-to-use, portable GUI components for building applications. It is a powerful tool for creating intuitive and interactive user interfaces.

Before we start, let’s make sure you have Python installed on your computer. Tkinter comes with the standard Python installation, so you don’t need to install any additional packages to use it.

Step 1: Setting up the Tkinter window

To begin, we need to create a Tkinter window and set up the basic layout of our data entry form. Here is the code to do just that:

import tkinter as tk

root = tk.Tk()
root.title("Data Entry Form")
root.geometry("400x300")

# Create a frame to hold the form fields
form_frame = tk.Frame(root)
form_frame.pack(padx=20, pady=20)

# Create labels and entry fields for each data point
tk.Label(form_frame, text="Name:").grid(row=0, column=0, padx=10, pady=5)
name_entry = tk.Entry(form_frame)
name_entry.grid(row=0, column=1)

tk.Label(form_frame, text="Age:").grid(row=1, column=0, padx=10, pady=5)
age_entry = tk.Entry(form_frame)
age_entry.grid(row=1, column=1)

tk.Label(form_frame, text="Email:").grid(row=2, column=0, padx=10, pady=5)
email_entry = tk.Entry(form_frame)
email_entry.grid(row=2, column=1)

root.mainloop()

In the code above, we first import the Tkinter module and create a Tk root window with the title "Data Entry Form" and a size of 400×300. We then create a frame to hold the form fields using the tk.Frame class and pack it onto the root window with padding.

Next, we create labels and entry fields for each data point (name, age, and email) using the tk.Label and tk.Entry classes. We set the grid layout for the form fields using the grid method, which allows us to position each element in a specific row and column within the form frame.

Step 2: Adding buttons for actions

Now that we have created the data entry form, let’s add buttons for user actions such as submitting the form and clearing the input fields. Here is the updated code:

import tkinter as tk

def submit_form():
    name = name_entry.get()
    age = age_entry.get()
    email = email_entry.get()

    print(f"Name: {name}, Age: {age}, Email: {email}")

def clear_form():
    name_entry.delete(0, tk.END)
    age_entry.delete(0, tk.END)
    email_entry.delete(0, tk.END)

root = tk.Tk()
root.title("Data Entry Form")
root.geometry("400x300")

form_frame = tk.Frame(root)
form_frame.pack(padx=20, pady=20)

tk.Label(form_frame, text="Name:").grid(row=0, column=0, padx=10, pady=5)
name_entry = tk.Entry(form_frame)
name_entry.grid(row=0, column=1)

tk.Label(form_frame, text="Age:").grid(row=1, column=0, padx=10, pady=5)
age_entry = tk.Entry(form_frame)
age_entry.grid(row=1, column=1)

tk.Label(form_frame, text="Email:").grid(row=2, column=0, padx=10, pady=5)
email_entry = tk.Entry(form_frame)
email_entry.grid(row=2, column=1)

submit_button = tk.Button(root, text="Submit", command=submit_form)
submit_button.pack(pady=10)

clear_button = tk.Button(root, text="Clear", command=clear_form)
clear_button.pack()

root.mainloop()

In the updated code, we define two functions submit_form and clear_form to handle the actions of submitting the form data and clearing the input fields, respectively. The submit_form function retrieves the input values from the entry fields and prints them to the console.

We then create two buttons, "Submit" and "Clear", using the tk.Button class and assign the command parameter to call the respective functions when the buttons are clicked. Finally, we pack the buttons onto the root window with padding.

Step 3: Ensuring a responsive layout

To make our data entry form layout responsive, we can use the grid method to set row and column weights so that the form elements adjust their size and position when the window is resized. Here is the updated code with responsive layout:

import tkinter as tk

def submit_form():
    name = name_entry.get()
    age = age_entry.get()
    email = email_entry.get()

    print(f"Name: {name}, Age: {age}, Email: {email}")

def clear_form():
    name_entry.delete(0, tk.END)
    age_entry.delete(0, tk.END)
    email_entry.delete(0, tk.END)

root = tk.Tk()
root.title("Data Entry Form")
root.geometry("400x300")

root.columnconfigure(0, weight=1)

form_frame = tk.Frame(root)
form_frame.pack(expand=True, padx=20, pady=20)

tk.Label(form_frame, text="Name:").grid(row=0, column=0, padx=10, pady=5)
name_entry = tk.Entry(form_frame)
name_entry.grid(row=0, column=1)

tk.Label(form_frame, text="Age:").grid(row=1, column=0, padx=10, pady=5)
age_entry = tk.Entry(form_frame)
age_entry.grid(row=1, column=1)

tk.Label(form_frame, text="Email:").grid(row=2, column=0, padx=10, pady=5)
email_entry = tk.Entry(form_frame)
email_entry.grid(row=2, column=1)

submit_button = tk.Button(root, text="Submit", command=submit_form)
submit_button.pack(pady=10)

clear_button = tk.Button(root, text="Clear", command=clear_form)
clear_button.pack()

root.mainloop()

In the updated code, we use the columnconfigure method to set the weight of the first column in the root window to 1. This allows the form elements to expand and fill the available space when the window is resized.

We also add the expand=True parameter when packing the form frame to make it resize with the window. This ensures that the form remains responsive and adjusts to different screen sizes.

That’s it! You have successfully created a responsive data entry form using Tkinter in Python. You can further customize and enhance the form by adding more widgets, validation, and error handling as needed. Tkinter provides a wide range of GUI components and functionalities to create intuitive and user-friendly interfaces for your applications.

0 0 votes
Article Rating
26 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@codefirstwithhala
1 month ago

I made a part 2 where you can insert the data into an excel sheet. Find it here: https://youtu.be/fvIThtPt6Nc 💻😊
Or… an alternative part 2 where you can insert this data into an SQLite database. Link: https://youtu.be/gdDI_GhIRGo

@SyedAyazShah-r2k
1 month ago

I don't comment on videos but this is special! 🎉

@osmanrashid4188
1 month ago

Great tutorial, Thank you

@tommywalsh-b5x
1 month ago

WOW! at last an excellent tutor who clearly explains what's going on. definitely a breath of fresh air. Thank you Hala.

@AlanSilva-zg6ui
1 month ago

Excelente conteúdo! Hala

@benjaminsaulnier4981
1 month ago

First course of yours I have followed.
Usually I don't leave comments but I have to say that your course is great ! thank you so much !
Very clear, easy to understand, you are very efficient. Congratulations.
I'm going to watch other your other courses for sure 😉

@KofiKwakye-ei6bm
1 month ago

Great teacher 🎉bravo

@MartinHAndersen
1 month ago

terrible ui framework. I will never use it. But thanks for a good presentation

@prathambhattarai5406
1 month ago

Completed the whole project and proud of myself and Thank you very much . You are such a great teacher and I can tell that through my years of learning experience

@ThomasMartin-m9x
1 month ago

Hall Linda Harris Elizabeth Brown Donna

@alfanalfian6077
1 month ago

you save me

@tarekfahmy1803
1 month ago

from my deeply heart really great job . thank you Hala

@srinivasan3642
1 month ago

Hi I am getting the following error message

{File "d:python_projectstkinterdata_entry.py", line 39, in enter_data

workbook = openpyxl.workbook()

^^^^^^^^^^^^^^^^^^^

TypeError: 'module' object is not callable}

@joeb895
1 month ago

I’ve been teaching myself Python for four months now and this video is excellent. Your explanation of everything helped me learn and understand a lot. Thank you! 😊

@duangaolu
1 month ago

You are really good! Thanks.

@ghaithshaqra4100
1 month ago

excellent tutorial
python is easy and very powerful but most of the people who try to explain and teach it make it harder to understand for learners
you made it simpler
thanx a lot

@ranjanakoul144
1 month ago

Hala, thanks for this Tutorial, I am trying to create a Vendor Database, however, how do avoid entering vendor name multiple times?

@horkacemedia6028
1 month ago

You have done the best tutorial in Python Programming. There is no another programmer which/who I can compare with you.

I appreciate you for this tutorial, thank you so much.

Just, I only watched it; but I wanna practice it in the coming days. Thanks

@Limitless_Technologies
1 month ago

Very good teacher.

@newlookak47
1 month ago

Can the data be placed into another window?