Python for Standalone Applications

Tkinter_SQLlite3_Connectivity

STEPS TO CONNECT TKINTER WITH DATABASE

Step 1:Create Database

Step 2:Create Table

Step 3:Insert data into table

Step 4:Display data from table

Step 5:Create function called Login()

Step 6:Connect Login()with SQLite & tkinter GUI

In [ ]:
#STEP1
import  sqlite3
conn=sqlite3.connect("student.db")
print("Database created successfully")
In [ ]:
#STEP 2
import  sqlite3
conn=sqlite3.connect("student.db")
print("Database Opened successfully")
conn.execute("""
CREATE TABLE ADMIN2(
ADMIN_ID INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL ,
USERNAME TEXT NOT NULL, 
PASSWORD TEXT NOT NULL)
""")
print ("Table ADMIN created successfully")
In [ ]:
#STEP 3
import  sqlite3
conn=sqlite3.connect("student.db")
print("Database Opened successfully")

conn.execute("INSERT INTO ADMIN2(USERNAME,PASSWORD) VALUES ('admin', 'admin007')");

conn.execute("INSERT INTO ADMIN2(USERNAME,PASSWORD) VALUES ('salma', 'salma007')");

conn.commit()
print ("Records inserted successfully")
conn.close()
In [ ]:
#STEP 4
#import library
import sqlite3
#open database
conn = sqlite3.connect('student.db')
#display recrod
cursor = conn.execute("SELECT * from ADMIN2")
print("ID\tUSERNAME\tPASSWORD")
for row in cursor:
   print ("{}\t{}\t\t{}".format(row[0],row[1],row[2]))
conn.close()
In [ ]:
#STEP 5:
from tkinter import *
#import library
import sqlite3
#open databse

#defining login function
def login():
    #getting form data
    uname=username.get()
    pwd=password.get()
    #applying empty validation
    if uname=='' or pwd=='':
        message.set("fill the empty field!!!")
    else:
      #open database
      conn = sqlite3.connect('student.db')
      #select query
      cursor = conn.execute('SELECT * from ADMIN2 where USERNAME="%s" and PASSWORD="%s"'%(uname,pwd))
      #fetch data 
      if cursor.fetchone():
       message.set("Login success")
      else:
       message.set("Wrong username or password!!!")
In [ ]:
##Step 6
from tkinter import *
#import library
import sqlite3

#defining loginform function
def Loginform():
    global login_screen
    login_screen = Tk()
    #Setting title of screen
    login_screen.title("www.Ummesalmam.com")
    #setting height and width of screen
    login_screen.geometry("350x250")
    login_screen["bg"]="#1C2833"
    #declaring variable
    global  message;
    global username
    global password
    username = StringVar()
    password = StringVar()
    message=StringVar()
    #Creating layout of login form
    Label(login_screen,width="300", text="Login From", bg="#0E6655",fg="white",font=("Arial",12,"bold")).pack()
    #Username Label
    Label(login_screen, text="Username * ",bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=20,y=40)
    #Username textbox
    Entry(login_screen, textvariable=username,bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=120,y=42)
    #Password Label
    Label(login_screen, text="Password * ",bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=20,y=80)
    #Password textbox
    Entry(login_screen, textvariable=password ,show="*",bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=120,y=82)
    #Label for displaying login status[success/failed]
    Label(login_screen, text="",textvariable=message,bg="#1C2833",fg="white",font=("Arial",12,"bold")).place(x=95,y=120)
    #Login button
    Button(login_screen, text="Login", width=10, height=1, command=login, bg="#0E6655",fg="white",font=("Arial",12,"bold")).place(x=125,y=170)
    login_screen.mainloop()
#calling function Loginform
Loginform()
In [ ]: