Create a Simple User Form with Python and Tkinter in 5 Minutes -A Beginner’s Guide
Last Updated on August 7, 2023 by Editorial Team
Author(s): Vaishnavi Seetharama
Originally published on Towards AI.
Today, It has become a trend for all businesses to go ‘Digital’. Whether they are small or Big, and it is also handy to use applications rather than collecting information manually. It looks professional as well. May it be in a Doctor's shop or in a grocery store, where you expect a customer to visit you again and again.
In this tutorial, I will show how to create a very simple user form using Python and Tkinter, a popular GUI toolkit. By the end of this blog, you’ll be equipped with the skills to develop your registration forms for various applications.
I like to create different GUIs for my Machine learning projects. It gives me freedom of expression to communicate with my end user. Creating a GUI myself for my Machine learning Projects feels like, I am telling the story myself more than anyone else telling my story.
There are many libraries that you can use to build a GUI in Python. Some famous ones are PyQt5, Kivy, PySimpleGUI, Tkinter, etc. I have used Tkinter as it is a simple and basic GUI library that is best for beginners in my opinion. Although there are some limitations to building GUI using Tkinter when it comes to styling your GUI, I use it for quick projects.
I am also a fan of web apps, but you will need your web server to distribute them if you don't want to publicly disclose your apps. You can find a tutorial for creating a web app using Streamlit here
Instead, I find it easy to create a GUI using Tkinter and convert it to an executable and distribute it to the end user just by sharing the file. it is quick and economical in terms of time and cost.
Prerequisites:
You should have Python and Tkinter installed on your system. If you don’t have Tkinter installed, you can install it using pip in Anaconda prompt:
pip install tk
Step 1: Importing the required libraries
As always, you will need to import all the libraries required for the project. We need tkinter and openpyxl packages for this simple project. You can enhance your code based on your need.
import openpyxl
from tkinter import *
from tkinter import messagebox
Step 2: Initialize the tkinter window
I have named it root, you can name it anything. You can change the title as per your requirement inside ‘root.title()’. ‘root.geometry()’ defines the size of the Tkinter window. The first part describes the width, and the second part refers to height. You can modify it as per your requirement. Here, I have defined a square window.
You can also change the tkinter Icon. But, we will not be discussing it in this blog.
# Create the main tkinter window
root = Tk()
root.title("Registration Form")
root.geometry('300x300')
Step 3: Create Labels and Entry Fields
I have created fields for First Name, Last Name, Email, and Mobile Number. You can add or remove fields as per your need.
You can add Check Buttons, Radio Buttons, Menu, Combo Box, etc to enhance your user form.
# Create labels and entry fields for each input
first_name_label = Label(root, text="First Name:")
first_name_label.pack()
first_name_entry = Entry(root)
first_name_entry.pack()
Last_name_label = Label(root, text="Last Name:")
Last_name_label.pack()
Last_name_entry = Entry(root)
Last_name_entry.pack()
email_label = Label(root, text="Email:")
email_label.pack()
email_entry = Entry(root)
email_entry.pack()
Mobile_label = Label(root, text="Mobile:")
Mobile_label.pack()
Mobile_entry = Entry(root)
Mobile_entry.pack()
For positioning the labels and the entry field, you can use different methods such as pack, grid, and place.
pack() positions widgets in horizontal and vertical boxes that are limited to left, right, top, and bottom positions. Each box is offset and relative to each other. I feel the limitations are higher compared to other methods.
grid() positions widgets in a two-dimensional grid of rows and columns similar to a spreadsheet. Although it is easier to understand the logic of positioning in this method, It is difficult to control the space between the rows and columns.
place() positions widgets in a two-dimensional grid using x and y absolute coordinates. You can define the position by defining x and y coordinates or by defining relative x and relative y coordinates.
Although my favorite is the ‘place()’ method as it allows you to position exactly where you need it, I have used the ‘pack()’ method in this blog to keep the guide simpler for beginners.
Maybe in another blog, I will discuss in detail what are all the differences in each method.
Step 4: Create a Function to get data from Entry Fields
Here, I want to get the data from the GUI and save the data in Microsoft Excel for future use. You can modify the data to change the saving method as per your need.
def register():
# Get the user input from the form
first_name = first_name_entry.get()
Last_name = Last_name_entry.get()
email = email_entry.get()
Mobile = Mobile_entry.get()
# Create a new row with the user input
new_row = [first_name, Last_name, email,Mobile]
# Append the new row to the Excel sheet
workbook = openpyxl.load_workbook("registration_data.xlsx")
sheet = workbook.active
sheet.append(new_row)
workbook.save("registration_data.xlsx")
messagebox.showinfo("Success", "Registration successful!")
Step 5: Create Submit Button and run the main loop
register_button = Button(root, text="Register", command=register)
register_button.pack()
root.mainloop()
This is the last step to create a user form GUI for registration. You can package this code as an executable file for an end user to run or You can enhance it to make a complete application that does much more fancy stuff.
Full code for your use:
import openpyxl
from tkinter import *
from tkinter import messagebox
def register():
# Get the user input from the form
first_name = first_name_entry.get()
Last_name = Last_name_entry.get()
email = email_entry.get()
Mobile = Mobile_entry.get()
# Create a new row with the user input
new_row = [first_name, Last_name, email,Mobile]
# Append the new row to the Excel sheet
workbook = openpyxl.load_workbook("registration_data.xlsx")
sheet = workbook.active
sheet.append(new_row)
workbook.save("registration_data.xlsx")
messagebox.showinfo("Success", "Registration successful!")
# Create the main tkinter window
root = Tk()
root.title("Registration Form")
root.geometry('300x300')
# Create labels and entry fields for each input
first_name_label = Label(root, text="First Name:")
first_name_label.pack()
first_name_entry = Entry(root)
first_name_entry.pack()
Last_name_label = Label(root, text="Last Name:")
Last_name_label.pack()
Last_name_entry = Entry(root)
Last_name_entry.pack()
email_label = Label(root, text="Email:")
email_label.pack()
email_entry = Entry(root)
email_entry.pack()
Mobile_label = Label(root, text="Mobile:")
Mobile_label.pack()
Mobile_entry = Entry(root)
Mobile_entry.pack()
register_button = Button(root, text="Register", command=register)
register_button.pack()
root.mainloop()
Please remember that this is a very basic code. You may want to use data validation techniques to use in real use cases.
Congratulations! You have your first User form ready.
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI