Simple registration form using Python Tkinter (original) (raw)
Last Updated : 08 May, 2025
**Tkinter is Python’s standard GUI (Graphical User Interface) library and **OpenPyXL is a module that allows for reading and writing Excel files. This guide shows you how to create a simple registration form with Tkinter, where users enter their details and those details are written into an Excel file. This form will automatically store the information in a pre-existing Excel file.
Steps to create the registration form
- **Create an Excel file: Make sure you have an empty Excel file (excel.xlsx) created and accessible. This file will store the registration data.
- **Create the Tkinter Window: We’ll build a basic form with fields like Name, Course, Semester, Form Number, Contact Number, Email and Address.
Python code
Python `
from openpyxl import load_workbook from tkinter import *
wb = load_workbook('C:\Users\Admin\Desktop\excel.xlsx') ws = wb.active
Set header row
def init_excel(): headers = ["Name", "Course", "Semester", "Form No.", "Contact No.", "Email", "Address"] for i, h in enumerate(headers, 1): ws.cell(row=1, column=i).value = h wb.save('C:\Users\Admin\Desktop\excel.xlsx')
Add form data
def insert_data(): if all(f.get() for f in entries): row = ws.max_row + 1 for i, f in enumerate(entries, 1): ws.cell(row=row, column=i).value = f.get() wb.save('C:\Users\Admin\Desktop\excel.xlsx') clear_fields() else: print("Please fill all fields.")
Clear form
def clear_fields(): for f in entries: f.delete(0, END)
Move to next field
def focus_next(entry): entry.focus_set()
init_excel() root = Tk() root.title("Registration Form") root.geometry("500x300") root.config(bg="light green")
labels = ["Name", "Course", "Semester", "Form No.", "Contact No.", "Email", "Address"] entries = [Entry(root) for _ in labels]
for i, lbl in enumerate(labels): Label(root, text=lbl, bg="light green").grid(row=i+1, column=0) entries[i].grid(row=i+1, column=1, ipadx=100) if i < len(labels) - 1: entries[i].bind("", lambda e, nf=entries[i+1]: focus_next(nf))
Button(root, text="Submit", fg="black", bg="red", command=insert_data).grid(row=len(labels)+1, column=1)
root.mainloop()
`
**Output
**Explanation:
- Loads an existing Excel file and sets up the active worksheet to store form data.
- **init_excel() writes column headers (Name, Course, etc.) in the first row of the Excel sheet.
- Creates a Tkinter window with labeled input fields for user details like Name, Course, Contact, etc.
- On clicking the “Submit” button, the **insert_data() checks if all fields are filled, then appends the data to the next row in the Excel file.
- Pressing Enter moves to the next input field and after submission, fields are cleared using clear_fields().
- The form window stays open using mainloop() until the user closes it.