login page in python Tkinter with database: System Login Project
Today we have to develop a login page in Python Tkinter with a database. We need to know at this time. Python language is the most commonly used language. And is in high demand. The Python project is also in high demand. So today we will create “The Login System” in Python.
About downloading and installing Python click here
How to make login page in python Tkinter with database?
The whole process as Login page in short can be broken down into the some simple steps:
How to make a login page in python Tkinter with the database. Firstly we will import all the necessary modules. Then we will make the main window and inside the main window. We will make the label, button, and entry. The main two button one for the login system and another for creating an account. After that, we will make their function to command our buttons. Here is the login page code in python Tkinter.
We are importing some Tkinter modules they are recommended such as pymsql , and the messagebox, or other if you have not to install pymysql run python -m pip install pymysql in your terminal.
from tkinter import * #we are importing tkinter module import pymysql as sql # here we are connection database with python through pymysql module from tkinter import messagebox #here we are importing messagebox
we have to configure the main window and provide proper color according to the requirement for better UI Experience and provide a shape such as width or hight.
root = Tk() #making main window root.configure(background='skyblue') #providing color to background image root.geometry('1500x900') #providing shape
All this comes at last so put this, at last, here I am putting only to understand you better. So here we are making the main function for our login and create account button. In this code, We have to maintain some text configuration and welcome messages.
#ALL THIS COME AT LAST SO PUT THIS AT LAST, HERE I AM PUTTING ONLY TO UNDERSTAND YOU BETTER #SO HERE WE ARE MAKING MAIN FUNCTION FOR OUR LOGIN AND CREATE ACCOUNT BUTTON. def main(): head = Label(root,text = 'WELCOME TO LOGIN SYSTEM',font = ('TIMES NEW ROMAN',40),bg='skyblue') head.place(x=320,y=200) login = Button(root,text='Login',width=35,height=3,bd=10,bg='red',font = ('TIMES NEW ROMAN',10),command=login_in) login.place(x=600,y=380) acc=Button(root,text='Create Account',width=35,height=3,bd=10,bg='red',font = ('TIMES NEW ROMAN',10),command=create) acc.place(x=600,y=480) root.mainloop() main()
here we are working on acc button to make acc button work ,so we make function here
#HERE WE ARE WORKING ON ACC BUTTON TO MAKE ACC BUTTON WORK ,SO WE MAKE FUNCTION HERE global E #global is metohd which can change variable value anytime global E1 global E2 global E3 global p global p1 global p2 global p3 def create(): top1 = Toplevel() #this will make another window inside main window. top1.geometry('500x500') top1.configure(background='skyblue') head = Label(top1,text = 'CREATE ACCOUNT',font = ('TIMES NEW ROMAN',30),bg='skyblue') head.place(x=50,y=30) l = Label(top1, text='Name',font = ('TIMES NEW ROMAN',25), bg = 'skyblue') l.place(x=10,y=100) E=Entry(top1,bd=10,width=20, textvariable=p, font = ('arial',20),bg='white') E.place(x=150,y=90) l1 = Label(top1, text='Phone_no',font = ('TIMES NEW ROMAN',25), bg = 'skyblue') l1.place(x=10,y=170) E1=Entry(top1,bd=10,width=20, textvariable=p1, font = ('arial',20),bg='white') E1.place(x=150,y=160) l2 = Label(top1, text='Email_ID',font = ('TIMES NEW ROMAN',25), bg = 'skyblue') l2.place(x=10,y=240) E2=Entry(top1,bd=10,width=20, textvariable=p2, font = ('arial',20),bg='white') E2.place(x=150,y=230) l3 = Label(top1, text='Password',font = ('TIMES NEW ROMAN',25), bg = 'skyblue') l3.place(x=10,y=310) E3=Entry(top1,bd=10,width=20, textvariable=p3, font = ('arial',20),bg='white') E3.place(x=150,y=300) submit = Button(top1,text='create',width=25,height=3,bd=10,bg='red',command=create_as) submit.place(x=130,y=400)
In this step we have add text variable in entry box and given a some coditions
p=StringVar() #these are textvariable which is given in entry box p1=StringVar() p2=StringVar() p3=StringVar() def create_as(): t = (str(p.get())) # here we get these textvariable in a variable. t1 = (str(p1.get())) t2 = (str(p2.get())) t3 = (str(p3.get())) if t!=0 or t1!=0 or t2!=0 or t3!=0: #this condition tell that if any column is left empty it will give message messagebox.showerror("Error","All field are mandatory") #here message is given else: db = sql.connect('localhost','root','password','database_name') #here are connecting with database cursor=db.cursor() #it will bound connection with database y = "insert into tablename(Name,Phone_no,Email_ID,Password)values('%s','%s','%s','%s')"%(t,t1,t2,t3) #inserting data in table cursor.execute(y) #it will execute the value and insert value into database db.commit() db.close() messagebox.showinfo('success','successfully inserted') #if value is inserted ,it will give the message.
This time we have to work fro login system
#NOW WE WORK FOR LOGIN SYSTEM global r global r1 global E global E1 def login_in(): top= Toplevel() top.configure(background='skyblue') top.geometry('500x300') l = Label(top, text='LOGIN HERE',font = ('TIMES NEW ROMAN',25),bg= 'skyblue') l.place(x=10,y=10) l = Label(top, text='Username',font = ('TIMES NEW ROMAN',25),bg='skyblue') l.place(x=10,y=70) E=Entry(top,bd=10,width=20, textvariable=r, font = ('arial',20),bg='white') E.place(x=150,y=60) l1 = Label(top, text='Password',font = ('TIMES NEW ROMAN',25),bg= 'skyblue') l1.place(x=10,y=140) E1=Entry(top,bd=10,width=20, textvariable=r1, font = ('arial',20),bg='white') E1.place(x=150,y=130) submit = Button(top,text='login',width=30,bd=10,bg='red',height=3,command=login_as) submit.place(x=160,y=210)
Database consiguration with python
#NOW WE WILL FETCH VALUE FROM DATABASE FOR LOGIN r=StringVar() r1=StringVar() def login_as(): t= (str(r.get())) t1=(str(r1.get())) h=(t,t1) #here we are taking value into tuple db = sql.connect('localhost','root','password','database_name') cursor=db.cursor() y = ("select * from table where Name =%s and Password =%s ") #here %s will get the data result=cursor.execute(y,h) if result==True: # checking condition if value is database is true then it will fetch it top=Tk() top.geometry('1500x900') top.title('login details') top.configure(background = 'skyblue') else: ans=messagebox.showerror('error','login fail') root.quit()



If you are facing problem in creating this ‘LOGIN SYSTEM’ , You can ask in comment section.
now you login page is looks like ……
More Project (Python): Create a wonderful search bar GUI with Python.