Create window Registration form in Python and store data in Text File

Create Registration form and store data in a Text File.

Hello friends how are you, today in this blog i will teach you how you can create a Registration Form in Python using Tkinter GUI Library. This tutorial will help you definitely if you wan to create a window application in Python. I am using PyCharm IDE and now i am going to describe everything step by step so just go through this post to get complete knowledge.

Registration form is one of the most used Form in Window or GUI Applications, It helps user to Register by using  name, contact, email, gender etc actually it depends upon the type of Registration.

Here in this post i am using name, contact, email, gender, city and state of user for Registration.

Here i have created a Registration form using Tkinter GUI library in which i have used name, contact, email, gender, city and state of user for Registration.

Registration Form Screenshot

when you run the code you will get screen like below


Registration Form with empty validation

I have applied a Empty Validation means if you left blank any field then you will get a message "fill the empty field!!!" after pressing button.


Registration Form with stored successfully

When you will fill all the fields and click on Register button then form data will be stored in a Text File. In my case File location is  E:/StuReg.txt  it means in E drive a Text File StuReg will be created and form data will be stored in it.


When you click on Register button you will get a message "Stored Successfully" and data will be stored in Text file.


Registration Students Record

Here i am providing the screenshot of file where all the records of registered students are present.

Widgets used to create Registration Form

1.Label
2.TextBox or EditText(Entry)
3.Radiobutton
4.Combobox
5.Button

Here is the Complete Registration Form

from tkinter import *
from  tkinter import  ttk
#defining register function
def register():
    #getting form data
    name1=name.get()
    con1=contact.get()
    email1=email.get()
    gen1=gender.get()
    city1=city.get()
    state1=state.get()
    #applying empty validation
    if name1=='' or con1==''or email1=='' or gen1==''or city1==''or state1=='':
        message.set("fill the empty field!!!")
    else:
        #opening of file to store data 
        fileptr = open("E:/StuReg.txt", "a")#here a means append
        if gen1==1:
         fileptr.write("\n"+name1+"  "+con1+"  "+email1+"  Male  "+city1+"  "+state1)
        else:
         fileptr.write("\n" + name1 + "  " + con1 + "  " + email1 + "  Female  " + city1 + "  " + state1)
        message.set("Stored successfully")

#defining Registrationform function
def Registrationform():
    global reg_screen
    reg_screen = Tk()
    #Setting title of screen
    reg_screen.title("Registration Form")
    #setting height and width of screen
    reg_screen.geometry("350x400")
    #declaring variable
    global  message;
    global name
    global contact
    global email
    global gender
    global city
    global state
    name = StringVar()
    contact = StringVar()
    email=StringVar()
    gender=IntVar()
    city=StringVar()
    state=StringVar()
    message=StringVar()
    #Creating layout of Registration form
    Label(reg_screen,width="300", text="Please enter details below", bg="orange",fg="white").pack()
    #name Label
    Label(reg_screen, text="Name * ").place(x=20,y=40)
    #name textbox
    Entry(reg_screen, textvariable=name).place(x=90,y=42)
    #contact Label
    Label(reg_screen, text="Contact * ").place(x=20,y=80)
    #contact textbox
    Entry(reg_screen, textvariable=contact).place(x=90,y=82)

    # email Label
    Label(reg_screen, text="Email * ").place(x=20, y=120)
    # email textbox
    Entry(reg_screen, textvariable=email).place(x=90, y=122)

    # gender Label
    Label(reg_screen, text="Gender * ").place(x=20, y=160)
    # gender radiobutton
    Radiobutton(reg_screen,text="Male",variable=gender,value=1).place(x=90,y=162)
    Radiobutton(reg_screen, text="Female", variable=gender, value=2).place(x=150, y=162)

    # city Label
    Label(reg_screen, text="City * ").place(x=20, y=200)
    # city combobox
    monthchoosen = ttk.Combobox(reg_screen, width=27, textvariable=city)
    monthchoosen['values'] = (' Mumbai',
                              ' Bhopal',
                              ' Patna',
                              ' Indore',
                              ' Nagpur',
                              ' Motihari',
                              ' Pune',
                              ' Gwalior',
                              ' Jabalpur',)
    monthchoosen.current()
    monthchoosen.place(x=90,y=202)

    # state Label
    Label(reg_screen, text="State * ").place(x=20, y=240)
    # state combobox
    monthchoosen = ttk.Combobox(reg_screen, width=27, textvariable=state)
    monthchoosen['values'] = (' Madhya Pradesh',
                              ' Maharashtra',
                              ' Bihar',
                              ' Punjab',
                              ' Gujrat',
                              ' Rajsthan',)
    monthchoosen.current()
    monthchoosen.place(x=90, y=242)
    #Label for displaying login status[success/failed]
    Label(reg_screen, text="",textvariable=message).place(x=95,y=264)
    #Login button
    Button(reg_screen, text="Register", width=10, height=1, bg="orange",command=register).place(x=105,y=300)
    reg_screen.mainloop()
#calling function Registrationform
Registrationform()

I hope this post will help you definitely, if you have any doubt then let me know by comment i will definitely fix it.
If you like my post then please share it with your friends. Thanks😊  

or
In this app you will get Complete Tutorials, Programs and Interview Questions/Answers 

Post a Comment

0 Comments