Registration Form – Python Source Code

  • Code With GUI
# Importing required modules
import tkinter as tk
from tkinter.ttk import Combobox
from tkinter import messagebox 

# Function To Check The Phone Number Is Valid Or Not
def Valid_PhoneNo(input_str):
    try:
        int(input_str)
        return len(input_str) == 10
    except ValueError:
        return False

# Function To Handle Form Submission
def register():
    Name = Name_Entry.get()
    Age = Age_Entry.get() 
    Gender = Gender_Value.get() 
    Address = Address_Entry.get()
    PhNo = PhNo_Entry.get()
    Email = Email_Entry.get()
    
    # Confirm The Input Fields
    if not Name or not Age or not Gender or not Address or not PhNo:
        messagebox.showerror("Error", "All fields are required")
    elif not Valid_PhoneNo(PhNo):
        messagebox.showerror("Error", "Invalid Phone Number. Please enter a 10-digit integer.")
    else:
        messagebox.showinfo("Registration", "Registration Successful!")
        
        # Save Registration Detail To txt File
        with open("Registeration Records.txt", "a") as rec:
            rec.write (f"\nName: {Name}\n")
            rec.write (f"Age: {Age}\n")
            rec.write (f"Gender: {Gender}\n")
            rec.write (f"Address: {Address}\n")
            rec.write (f"Phone No: {PhNo}\n")
            rec.write (f"Email: {Email}\n  ")

# Create The Main Root The Main The Root Of Window      
root = tk.Tk()
root.geometry("700x500")
root.title("Registration Form")

# Labeling The Form Elements
Form_Heading = tk.Label(root, text="Registartion Form",padx=20,borderwidth=5 , font=("arial", 30, "bold"))
Form_Heading.grid(row=0, column=3)

Name = tk.Label(root, text="Name *", font=("arial", 25))
Age = tk.Label(root, text="Age *", font=("arial", 25))
Gender = tk.Label(root, text="Gender *", font=("arial", 25))
Address = tk.Label(root, text="Address *", font=("arial", 25))
PhNo = tk.Label(root, text="Phone No *", font=("arial", 25))
Email = tk.Label(root, text="Email", font=("arial", 25))

# Packing The Form Elements Using Grid
Name.grid(row=1, column=0)
Age.grid(row=3, column=0)
Gender.grid(row=4, column=0)
Address.grid(row=5, column=0)
PhNo.grid(row=6, column=0)
Email.grid(row=7, column=0)

# Variable Holders For User Input
Name_Value = tk.StringVar()
Age_Value = tk.StringVar()
Gender_Value = tk.StringVar()
Address_Value = tk.StringVar()
PhNo_Value = tk.StringVar()
Email_Value = tk.StringVar()

# Setting Default Value For Gender
Gender_Value.set("No")

# List Of Age Option For The Combobox
Age_List = ["Below 13", "13-18", "18-25", "Above 25"]

# Usery Entry Widgets And Radio Buttons For Gender Section
Name_Entry = tk.Entry(root,font="arial 20" ,textvariable=Name_Value)
Age_Entry = Combobox(root,values=Age_List ,font="arial 20" , textvariable=Age_Value, state="readonly")
Gender_Frame = tk.Frame(root)
Gender_Entry_Male = tk.Radiobutton(Gender_Frame, text="Male",font="Arial 15" ,variable=Gender_Value, value="Male")
Gender_Entry_Female = tk.Radiobutton(Gender_Frame, text="Female",font="Arial 15", variable=Gender_Value, value="Female")
Gender_Entry_Others = tk.Radiobutton(Gender_Frame, text="Others",font="Arial 15", variable=Gender_Value, value="Others")
Address_Entry = tk.Entry(root,font="arial 20" , textvariable=Address_Value)
PhNo_Entry = tk.Entry(root,font="arial 20" , textvariable=PhNo_Value)
Email_Entry = tk.Entry(root,font="arial 20" , textvariable=Email_Value)

# Packing The User Entry Widgets
Name_Entry.grid(row=1, column=3)
Age_Entry.grid(row=3, column=3)
Gender_Frame.grid(row=4, column=3)
Gender_Entry_Male.grid(row=0, column=0)
Gender_Entry_Female.grid(row=0, column=1)
Gender_Entry_Others.grid(row=0, column=2)
Address_Entry.grid(row=5, column=3)
PhNo_Entry.grid(row=6, column=3)
Email_Entry.grid(row=7, column=3)

# Submit Button
Submit_Button = tk.Button(root, text="Submit", font="Arial 20 bold", command=register)
Submit_Button.grid(row=8, column=3)

# Start the main event loop
root.mainloop()

# Regestration Form Source Code - By Codotix

Leave a Comment

Your email address will not be published. Required fields are marked *