Login Form in Python GUI with MYSQL and MSSQL Connection

In python, if we want to Design Login Form with database authentication with MYSQL or MSSQL then we have to follow the following process with step by step guide. 

Here we will learn How to Design Login Form in Python GUI with MYSQL connection in Python or SQL Server Connection in Python
Design Login Form in Python Tkinter


First we have to Create table For authentication with following field Name:
Login_tbl
usrname passwrd
sko tech

Same type of table we create in MSSQL. 

After that we will use following tkinter widget:
Name Tkinter Wiidget
Bannerlabel  Label
UserLabel  Label
PassLabel  Label
UserTxt  Entry
PassTxt  Entry
LoginBtn  Button

After that we will proceed with following points:

  1. Login Form Design in Python GUI with MYSQL Connection
  2. Login Form Design in Python GUI with MSSQL Connection

Now we will first Start with MYSQL connection then MS SQL Server Connection.

(1). Login Form Design in Python GUI with MYSQL Connection :

Now, first we will import ‘mysql.connector’ for connection. Then write code for establish connection with MYSQl after that write code for query like following way:

import mysql.connector as mysqlconnector
import tkinter as mytk
from tkinter import *
import tkinter.messagebox as mymessagebox

MyLoginForm = mytk.Tk()
MyLoginForm.title('Login Form with MYSQL Connection')
#Set Form Size
MyLoginForm.geometry("380x200")

def ClicktoLogin():
    #MYSQL ConnectionString
    mydb = mysqlconnector.connect(host="xxx.xxx.xxx.xx", user="sko", password="mypassword", database="skotechlearn")
    mycursor = mydb.cursor(dictionary=True)
    mycursor.execute("SELECT * FROM login_tbl where usrname = '"+ UserTxt.get() +"' and passwrd = '"+ PassTxt.get() +"';")
    myresult = mycursor.fetchone()
    if myresult==None:
       mymessagebox.showerror("Error", "Invalid User Name And Password")

    else:
       mymessagebox.showinfo("Success", "Successfully Login")
            
    mydb.close()
    mycursor.close()

# Set Tkinter Widget Size Location and Style
Bannerlabel = Label(MyLoginForm, text = "Login Form......", width=40, bg= 'yellow')
Bannerlabel.place(x=20, y=20)

UserLabel = Label(MyLoginForm, text = "User Name:", width=10)
UserLabel.place(x=20, y=60)

UserTxt = Entry(MyLoginForm,  width=27, relief="flat")
UserTxt.place(x=120, y=60)

#Set Focus on User Entrybox in Tkinter
UserTxt.focus()

PassLabel = Label(MyLoginForm, text = "Password :", width=10)
PassLabel.place(x=20, y=90)

PassTxt = Entry(MyLoginForm,  width=27, relief="flat")
PassTxt.place(x=120, y=90)

#Set Password Char in Entry Widget
PassTxt.config(show="*");

LoginBtn = Button(MyLoginForm, text ="Login", command = ClicktoLogin, relief="groove", fg='blue')
LoginBtn.place(x=280, y=140)

MyLoginForm.configure(background='#54596d')
MyLoginForm.mainloop()

When we execute this code. We will see the output looks like bellow.
 
Login Form MYSQL Connection in Python



As you can see we have changed Form, Label color for better look. After successful Login there will be a message displayed like above figure.
Now, Lets come to another point with MSSQL Connection in Python.

(2). Login Form Design in Python GUI with MSSQL Connection :

For MSSQL Connection first we install pymssql.
pip install pymssql

or
pip3 install pymssql

After that we will import pymssql in python code.
 
For Import and for MSSQL Connection String in Python, we will write following code.
 
MSSQL Server Connection in Python

Connection String Code:

# pymssql.connect(MSSQLServerIP, UserName, Password, Database)
pymssql.connect("xxx.xxx.xxx.xx", "myusername", "myPassword", "MyDatabase")


Let’s see how can we connect MSSQL Server with Python Tkinter Login Form?

import pymssql as MSSQLCnn
import tkinter as mytk
from tkinter import *
import tkinter.messagebox as mymessagebox

MyLoginForm = mytk.Tk()
MyLoginForm.title('Login Form with MSSQL Server Connection')
MyLoginForm.geometry("380x200")

def ClicktoLogin():
    #MSSQL Server ConnectionString
    MSSQLdb = MSSQLCnn.connect("xxx.xxx.xxx.xx", "SQLServerUser", "SQlServerPassword", "SQLServerDatabase")
    mySQLcursor = MSSQLdb.cursor()
    mySQLcursor.execute("SELECT * FROM login_tbl where usrname = '"+ UserTxt.get() +"' and passwrd = '"+ PassTxt.get() +"';")
    mySQLresult = mySQLcursor.fetchone()
    if mySQLresult==None:
       mymessagebox.showerror("Error", "Invalid User Name And Password")

    else:
       mymessagebox.showinfo("Success", "Successfully Login")
            
    MSSQLdb.close()
    mySQLcursor.close()

Bannerlabel = Label(MyLoginForm, text = "MSSQL Server Login Form......", width=40, bg= '#81907E', fg='white')
Bannerlabel.place(x=20, y=20)

UserLabel = Label(MyLoginForm, text = "User Name:", width=10, bg='#FFE6D8')
UserLabel.place(x=20, y=60)

UserTxt = Entry(MyLoginForm,  width=27, relief="flat")
UserTxt.place(x=120, y=60)

#Focus of User Entrybox in Tkinter
UserTxt.focus()

PassLabel = Label(MyLoginForm, text = "Password :", width=10, bg='#FFE6D8')
PassLabel.place(x=20, y=90)

PassTxt = Entry(MyLoginForm,  width=27, relief="flat")
PassTxt.place(x=120, y=90)

#Set Entry box Password Char in Tkinter
PassTxt.config(show="*");

LoginBtn = Button(MyLoginForm, text ="Login", command = ClicktoLogin, relief="groove", fg='blue')
LoginBtn.place(x=280, y=140)

MyLoginForm.configure(background='#FFE6D8')
MyLoginForm.mainloop()


After the execution of this code in python, the following output will present.

Login Form MSSQL Connection in Python


This way you can easily Create or Design Login Form in Python GUI with Tkinter and Connect Login Form with MSSQL in Python Or Connect Login Form with MYSQL in Python.

0 comments: