Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to make a login system in tkinter using MySQL database?

 



database.py
import mysql.connector
import sys

def Connect():
    conn=None
    try:
        conn=mysql.connector.Connect(
            host='localhost',
            username='root',
            password='',
            database='hancie'
        )


    except:
        print('Error', sys.exc_info())

    finally:
        return conn

loginbackend.py
from backend.database import Connect
import sys

def login(id):
    conn=None
    sql="""SELECT * FROM test720 WHERE email=%s AND password=%s"""
    values=(id.getEmail(), id.getPassword())
    result=None
    try:
        conn=Connect()
        cursor=conn.cursor()
        cursor.execute(sql, values)
        result=cursor.fetchone()
        cursor.close()
        conn.close()

    except:
        print('Error',sys.exc_info())

    finally:
        del values, sql, conn
        return result
customerlibrary.py

class CustomerLibs():
    def __init__(self, id=0, email=None, password=None):
        self.id=id
        self.email=email
        self.password=password

    def getId(self):
        return self.id

    def getEmail(self):
        return self.email

    def getPassword(self):
        return self.password


    def setId(self, id):
        self.id=id

    def setEmail(self, email):
        self.email=email

    def setPassword(self, password):
        self.password=password

    def __str__(self):
        return ('{},{},{}'.format(self.id, self.email,self.password))
login.py
from tkinter import *
from tkinter import messagebox
from backend.loginbackend import login
from middleware.customerlibrary import CustomerLibs


class LoginGUI():
    def __init__(self,root):
        self.root=root
        self.root.title('Hancie e-Learning Studio LOGIN')
        self.root.geometry('500x400')
        self.root.resizable(width=0, height=0)

        frame=Frame(self.root, bg='green', height=60)
        frame.pack(side=TOP, fill=BOTH)

        titlelabel=Label(frame, text='LOGIN SYSTEM', font=('Times New Roman',16,'bold'))
        titlelabel.place(relx=0.5, rely=0.5, anchor=CENTER)

        emailabel=Label(self.root, text="Email:", font=('Times New Roman',14))
        emailabel.place(x=60,y=100)

        emailtext=Entry(self.root, font=('Times New Roman',14))
        emailtext.place(x=200, y=100)

        passwordlabel=Label(self.root, text="Password:", font=('Times New Roman',14))
        passwordlabel.place(x=60, y=150)

        passwordtext=Entry(self.root, font=('Times New Roman',14))
        passwordtext.place(x=200, y=150)

        def loginlogic():
            customerdata=CustomerLibs(email=emailtext.get(), password=passwordtext.get())
            result=login(customerdata)
            if result!=None:
                messagebox.showinfo("Hancie","Welcome {}".format(emailtext.get()))

            else:
                messagebox.showerror("Hancie","Incorrect email and password")


        loginbtn=Button(self.root, text="Login",command=loginlogic, font=('Times New Roman',14))
        loginbtn.place(x=200, y=200)



if __name__=='__main__':
    root=Tk()
    LoginGUI(root)
    root.mainloop()
Output

Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.