SIGNUP PAGE IN PYTHON TKINTER
- Jayesh Malviya
- Sep 14, 2022
- 2 min read
INTRODUCTION
Hello everyone today we create a signup page in python Tkinter and save data using the python dictionary. So let's start with small steps.
step1: import all necessary modules, I use only two modules of Tkinter.
from tkinter import *
from tkinter import messagebox
step2: Create the root and set widow size using the geometry function.
root = Tk()
root.geometry("600x500")
step3: create two functions name signup and process, and then in the signup function create a label for the heading, and also create all labels like username, emails, password, and re-enter the password.
def signup():
Label(root, text="SIGNUP PAGE", font="Ariel 20 bold", bg="skyblue").pack(fill="x", side="top")
user = Label(root, text="Username")
user.place(x=30, y=80)
email = Label(root, text="Email")
email.place(x=30, y=140)
pas1 = Label(root, text="Password")
pas1.place(x=30, y=200)
pas2 = Label(root, text="Re-Enter password")
pas2.place(x=30, y=260)
step4: After that, we create an entry widget where users can input our information, and create a button that calls a process() function.
# entry widgets
signup.name = Entry(root)
signup.name.place(x=220, y=80)
signup.mail = Entry(root)
signup.mail.place(x=220, y=140)
signup.pas = Entry(root)
signup.pas.place(x=220, y=200)
signup.repas = Entry(root)
signup.repas.place(x=220, y=260)
# signup button which call process function
Button(root, text="Signup", command=process).place(x=30, y=320)
step5: After that, we edit the process function to get the values and save them in the dictionary, and before saving we create some conditions for input to take valid details.
def process():
# get values.
username = signup.name.get()
useremail = signup.mail.get()
userpass = signup.pas.get()
reEnter = signup.repas.get()
in that, we get all values from the user using the get function, get function can use to collect values from the entry widget, next we see some conditions if all conditions are false then it's saved in the dictionary.
# conditions
if username == "": # if username is null then messagebox is show.
messagebox.askretrycancel("required", "fill username")
elif useremail == "" or "@gmail.com" not in useremail:
messagebox.askretrycancel("Invalid gmail", "please enter valid email.")
elif userpass == "" or userpass != reEnter:
messagebox.askretrycancel("Invalid password", "please enter valid password")
else:
db = {
"Username":username,
"email":useremail,
"password":userpass
}
print(db)
"db" is the dictionary that holds data and when we print this it looks like that:
{'Username': 'Jayesh', 'email': 'Jayesh@gmail.com', 'password': 'Jayesh123'}
so let's see the complete code after that we see output of the code.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("600x500")
# signup system.
def signup():
Label(root, text="SIGNUP PAGE", font="Ariel 20 bold", bg="skyblue").pack(fill="x", side="top")
user = Label(root, text="Username")
user.place(x=30, y=80)
email = Label(root, text="Email")
email.place(x=30, y=140)
pas1 = Label(root, text="Password")
pas1.place(x=30, y=200)
pas2 = Label(root, text="Re-Enter password")
pas2.place(x=30, y=260)
# entry widgets
signup.name = Entry(root)
signup.name.place(x=220, y=80)
signup.mail = Entry(root)
signup.mail.place(x=220, y=140)
signup.pas = Entry(root)
signup.pas.place(x=220, y=200)
signup.repas = Entry(root)
signup.repas.place(x=220, y=260)
# signup button which call process function
Button(root, text="Signup", command=process).place(x=30, y=320)
def process():
# get values.
username = signup.name.get()
useremail = signup.mail.get()
userpass = signup.pas.get()
reEnter = signup.repas.get()
# conditions
if username == "": # if username is null then messagebox is show.
messagebox.askretrycancel("required", "fill username")
elif useremail == "" or "@gmail.com" not in useremail:
messagebox.askretrycancel("Invalid gmail", "please enter valid email.")
elif userpass == "" or userpass != reEnter:
messagebox.askretrycancel("Invalid password", "please enter valid password")
else:
db = {
"Username":username,
"email":useremail,
"password":userpass
}
print(db)
signup()
root.mainloop()
Output is -

So we hope this is helpful for you.
Thanks for watching.
Comentários