Python 3 Tkinter Login and Registration System GUI Script in MySQL Database Using mysql-connector Library Desktop App Full Project For Beginners

Python 3 Tkinter Login and Registration System GUI Script in MySQL Database Using mysql-connector Library Desktop App Full Project For Beginners

 

Welcome folks today in this tutorial we will be looking at how we can build a login and registration system desktop app in tkinter using mysql database and also mysql-connector library in python. All the full source code of the application will be given below.

 

 

 

Screenshots

 

 

 

You can see the following screenshots of the desktop app that we will be building in this post

 

It can register users inside the database

It can login users inside the application

 

 

 

 

Get Started

 

 

Now to get started you need to install the following libraries inside your python project using the pip command as shown below

 

pip install tkinter

 

pip install mysql

 

pip install mysql-connector

 

Now first of all we will be using the xampp apache server and it’s mysql database so just download it and install it inside your system

 

 

 

In the above screenshots we have turned on the apache server and mysql database and now create a new database and then inside the sql section copy paste this sql code which is given below to create a table schema

 

 

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `user` varchar(30) NOT NULL,
  `pass` text NOT NULL,
  `name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

After doing all the steps just make an app.py file and copy paste the following code to it as shown below

See also  Python 3 Script to Convert PDF Document to Images Using pdf2image Library Full Example

 

app.py

 

 

from tkinter import *
import mysql.connector as mysql
from tkinter.messagebox import *

class Login():
    def __init__(self):
        self.root = Tk()
        self.root.geometry("400x300")
        self.root.title("Login Window")
        self.create_elements()
        self.root.mainloop()
        
    def create_elements(self):
        
        self.username = Label(self.root, text="Username:", font=('Verdana', 14, 'bold'))
        self.username.place(x=50, y=50)

        self.entry_username = Entry(self.root, font=('Verdana', 14))
        self.entry_username.place(x=160, y=50)

        self.password = Label(self.root, text="Password:", font=('Verdana', 14, 'bold'))
        self.password.place(x=50, y=90)

        self.entry_password = Entry(self.root, font=('Verdana', 14))
        self.entry_password.place(x=160, y=90)

        self.login_button = Button(self.root, text="Login", height=2, width=10, command=self.login_user)
        self.login_button.place(x=50, y=160)

        self.new_user = Label(self.root, text="New User?", font=('Verdana', 10, 'bold'))
        self.new_user.place(x=150, y=140)

        self.register_button = Button(self.root, text="Sign Up", height=2, width=10, command=self.destroy_login)
        self.register_button.place(x=150, y=160)


    def destroy_login(self):
        self.root.destroy()
        register = Register()

    def login_user(self):
        username = self.entry_username.get()
        userpassword = self.entry_password.get()

        if(username == "" or userpassword == ""):
            showinfo("Oops!","Your information can't be empty!")
            return

        mydb = mysql.connect(
          host="localhost",
          user="root",
          password="",
          database = "tkinter_users"
        )

        mycursor = mydb.cursor()
        sql = "select user, pass from users where user=%s and pass=%s"
        val = (username, userpassword)
        mycursor.execute(sql, val)
        result = mycursor.fetchone()
        self.entry_username.delete(0, END)
        self.entry_password.delete(0, END)
        if result:
            showinfo("Success","You're logged in!")
        else:
            showinfo("Failed","You've entered wrong credentials!")


class Register():
    def __init__(self):
        self.root = Tk()
        self.root.geometry("500x300")
        self.root.title("Register Window")
        self.create_elements()
        self.root.mainloop()

    def create_elements(self):

        self.username = Label(self.root, text="Username:", font=('Verdana', 14, 'bold'))
        self.username.place(x=50, y=50)

        self.entry_username = Entry(self.root, font=('Verdana', 14))
        self.entry_username.place(x=160, y=50)

        self.password = Label(self.root, text="Password:", font=('Verdana', 14, 'bold'))
        self.password.place(x=50, y=90)

        self.entry_password = Entry(self.root, font=('Verdana', 14))
        self.entry_password.place(x=160, y=90)

        self.name = Label(self.root, text="Name:", font=('Verdana', 14, 'bold'))
        self.name.place(x=50, y=130)

        self.entry_name = Entry(self.root, font=('Verdana', 14))
        self.entry_name.place(x=160, y=130)

        self.register_button = Button(self.root, text="Sign Up", height=2, width=10, command=self.register_user)
        self.register_button.place(x=50, y=180)

        self.existing_user = Label(self.root, text="Existing User?", font=('Verdana', 10, 'bold'))
        self.existing_user.place(x=150, y=160)

        self.login_button = Button(self.root, text="Login", height=2, width=10, command=self.destroy_register)
        self.login_button.place(x=150, y=180)

    def destroy_register(self):
        self.root.destroy()
        login = Login()

    def register_user(self): 
        username = self.entry_username.get()
        userpassword = self.entry_password.get()
        name = self.entry_name.get()

        if(username == "" or userpassword == "" or name == ""):
            showinfo("Oops!","Your information can't be empty!")
            return

        mydb = mysql.connect(
          host="localhost",
          user="root",
          password="",
          database = "tkinter_users"
        )

        mycursor = mydb.cursor()

        mycursor.execute("select count(*) from users")
        result = mycursor.fetchone()
        old_count = result[0]

        sql = "INSERT INTO users (user, pass, name) VALUES (%s, %s, %s)"
        val = (username, userpassword, name)
        mycursor.execute(sql, val)
        mydb.commit()
        
        mycursor.execute("select count(*) from users")
        result = mycursor.fetchone()
        new_count = result[0]

        self.entry_username.delete(0, END)
        self.entry_password.delete(0, END)
        self.entry_name.delete(0, END)

        if(old_count + 1 == new_count):
            showinfo("Success","Your information is saved successfully!")
        else:
            showinfo("Failed","Your information couldn't save successfully!")


if __name__ == '__main__':
    login = Login()

 

See also  Python 3 WxPython Submit Form Data and Display it in Popup or Modal Window GUI Desktop App Full Project For Beginners

 

Now just type the below command to start the desktop app

 

python app.py

 

Leave a Reply