Hello python coders,
In this post you are going to see that how you can create a simple login page using python. To perform this activity you must have installed python on your machine.
Let's talk about coding, first of all you have to import some modules like - from tkinter import * , from venv import create, from email.mime import image, from cgitb import text. then you should proceed for creating a window.
To create a window you have to declare root = Tk() and then to appear as a window you have to form a loop using root.mainloop(). After doing this you will see a window named tk will appear on your screen.
Now you have to change your window name that is from tk to the which you like, for that you have to use root.title("Your fav name") this function will help you to write name of your window like if you want to name your window Python coder the you have to write root.title("Python coder") it will change your windows name from tk to Python code.
After changing the name you should try to change the icon of your window. Now, for doing this you have to type root.iconbitmap("login1.ico") in which login1.ico is an image file (.png .jpg .jpeg) which is further converted into icon file type , for the conversion of normal image to icon i have used a website which will helped me to convert my image to icon format and save that icon file with .ico extension.
After the successful insertion of icon you can insert any image or you can directly start form creation for login page.
for inserting the image you have to use the picture = PhotoImage(file="admin01.png")
lab = Label(root, image=picture) module to insert any image in your form,
where you can insert any image in format of .jpg, .png, .jpeg or any other supported format.
And now if want to and any thing else you can try for it.
Now you are ready to create the login form. We have to find out that what are the data are filled in login form,
like - email, Contact number, Unique ID, . and what are the data filled in password.
Generally username contains - email, mobile number or ID and password depends on user's choice.
For the creation of username we have to write - username = Label(root, text="Username", font="none 15", bg="#424949", fg="white")
and for the password - password = Label(root, text="Password", font="none 15", bg="#424949", fg="white"), these two lines of code help to ask user to enter the username and password.
And you have to close or pack these two functions otherwise you are not going to get a proper output, For closing these functions you have to write -username.grid(row=1, column=0, pady=10)
this finction will close the username and password.grid(row=2, column=0, pady=10) this function will close the password funtion. One more thing i would like to tell you that,
you can clearly see in the coding part its clearly mentioned...what are the font size , what are the font color , which color is backgrounf color and which color is foreground color.
Now you have to create an area where user can enter thair details, for doing this you need to create two boxes in which user can enter their details.
For creating the boxes you have to write - username_box = Entry(root, font="none 15", w=20) this function will help you to create the box for username and
password_box = Entry(root, font="none 15", w=20, show="*") this function will help you to create box for password, in this you can find show="*" this function helps you to hide your password.
Now as we have done previously, we have to close or pack this function. For closing the function we have to write -
username_box.grid(row=1, column=1, pady=10)this function will help you to close the username box and
password_box.grid(row=2, column=1, pady=10)this function will help you to close the password box.
As we have completed with form creation and getting details form user to fill in the form like username and password,
Now we have to create a login button, which help you to login to your accout if you have entered your details correctly.
for the login button creation - login = Button(root, text="Login", padx=30, pady=10) and as we have done previosly we have to close it or we have to pack it so, for closing this function we have to write -
login.grid(row=3, columnspan=2, column=0, pady=10) this function will close the login button function and helps to create the login button.
If you have done every thing in a proper way then you are going to get the output as you run your program or it shows error try to slove it.
I hope you people have understand how to create a login page using python.
Code -
from cgitb import text
from email.mime import image
from logging import root
from tkinter import *
from venv import create
root = Tk()
root.geometry("500x500")
root.configure(bg="#424949")
root.title("Login page")
root.iconbitmap("login1.ico")
picture = PhotoImage(file="admin01.png")
lab = Label(root, image=picture)
username = Label(root, text="Username", font="none 15", bg="#424949", fg="white")
password = Label(root, text="Password", font="none 15", bg="#424949", fg="white")
username_box = Entry(root, font="none 15", w=20)
password_box = Entry(root, font="none 15", w=20, show="*")
login = Button(root, text="Login", padx=30, pady=10)
lab.grid(row=0, column=0, columnspan=2, padx=95, pady=40)
username.grid(row=1, column=0, pady=10)
password.grid(row=2, column=0, pady=10)
username_box.grid(row=1, column=1, pady=10)
password_box.grid(row=2, column=1, pady=10)
login.grid(row=3, columnspan=2, column=0, pady=10)
root.mainloop()
Nice and informative.
ReplyDelete