Create Simple Login Application in Python Tutorial with Complete Source Code

Posts13

In this post you will learn about Create Simple Login Application in Python Tutorial with Complete Source Code. Read this below article carefully and understand this to put impact in real life.

Introduction

In this article, we’ll show you how to make a simple Python login application. The emphasis on code readability is part of the Python design philosophy. Python is therefore incredibly user-friendly, especially for those who are just starting out with programming. The syntax stresses readability and is fairly simple to pick up, which can speed up development time. So let’s start coding now.

Get To Start

The Python IDLE, or Integrated Development and Learning Environment, must first be downloaded and installed.

Installing SQLite Browser

We will now install SQLite and the DB Browser for SQLite after you have installed Python.

Taking in Modules

Run IDLE and select File > New File after installing and configuring the database. After then, a new window with a black file—the Python text editor—will appear.

Then duplicate the code I’ve provided below and paste it in IDLE.

from tkinter import *
import sqlite3

Putting the Main Frame together

We will now build the application’s primary frame after importing the components. Simply copy the code below and put it in the IDLE text editor to accomplish that.

root = Tk()
root.title("Python: Simple Login Application")
width = 400
height = 280
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)

Creating the Design

Following the development of the Main Frame, the application will now receive some layout. Please copy and paste the code below into the IDLE text editor.

#==============================VARIABLES======================================
USERNAME = StringVar()
PASSWORD = StringVar()
 
#==============================FRAMES=========================================
Top = Frame(root, bd=2,  relief=RIDGE)
Top.pack(side=TOP, fill=X)
Form = Frame(root, height=200)
Form.pack(side=TOP, pady=20)
 
#==============================LABELS=========================================
lbl_title = Label(Top, text = "Python: Simple Login Application", font=('arial', 15))
lbl_title.pack(fill=X)
lbl_username = Label(Form, text = "Username:", font=('arial', 14), bd=15)
lbl_username.grid(row=0, sticky="e")
lbl_password = Label(Form, text = "Password:", font=('arial', 14), bd=15)
lbl_password.grid(row=1, sticky="e")
lbl_text = Label(Form)
lbl_text.grid(row=2, columnspan=2)
 
#==============================ENTRY WIDGETS==================================
username = Entry(Form, textvariable=USERNAME, font=(14))
username.grid(row=0, column=1)
password = Entry(Form, textvariable=PASSWORD, show="*", font=(14))
password.grid(row=1, column=1)
 
#==============================BUTTON WIDGETS=================================
btn_login = Button(Form, text="Login", width=45, command=Login)
btn_login.grid(pady=25, row=3, columnspan=2)
btn_login.bind('<Return>', Login)

Establishing a database connection

The database function will then be created after the design has been set up. Simply copy the code below and put it in the IDLE text editor to accomplish that.

#==============================METHODS========================================
def Database():
    global conn, cursor
    conn = sqlite3.connect("pythontut.db")
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, username TEXT, password TEXT)")       
    cursor.execute("SELECT * FROM `member` WHERE `username` = 'admin' AND `password` = 'admin'")
    if cursor.fetchone() is None:
        cursor.execute("INSERT INTO `member` (username, password) VALUES('admin', 'admin')")
        conn.commit()

Developing the primary function

After successfully logging in, a new window will open. This is the primary function where the entry will be checked to see if a user exists in the database. Simply copy the code below and put it in the IDLE text editor to accomplish that.

def Login(event=None):
    Database()
    if USERNAME.get() == "" or PASSWORD.get() == "":
        lbl_text.config(text="Please complete the required field!", fg="red")
    else:
        cursor.execute("SELECT * FROM `member` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
        if cursor.fetchone() is not None:
            HomeWindow()
            USERNAME.set("")
            PASSWORD.set("")
            lbl_text.config(text="")
        else:
            lbl_text.config(text="Invalid username or password", fg="red")
            USERNAME.set("")
            PASSWORD.set("")   
    cursor.close()
    conn.close()
 
def HomeWindow():
    global Home
    root.withdraw()
    Home = Toplevel()
    Home.title("Python: Simple Login Application")
    width = 600
    height = 500
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    root.resizable(0, 0)
    Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
    lbl_home = Label(Home, text="Successfully Login!", font=('times new roman', 20)).pack()
    btn_back = Button(Home, text='Back', command=Back).pack(pady=20, fill=X)
 
def Back():
    Home.destroy()
    root.deiconify()

starting up the application

Save the program as “index.py” after the function is finished. This procedure will execute the code and verify that the main has been correctly initialized. Copy the code below, then paste it in the IDLE text editor to accomplish that.

#==============================INITIALIATION==================================
if __name__ == '__main__':
    root.mainloop()

You recently saw how we used Python to build a simple login application. I hope that this little course will enable you to learn more about Python programming. Please visit this website for further information and tutorials.

Download

Final Words

In this post you learnt about Create Simple Login Application in Python Tutorial with Complete Source Code. If you like this article please share with your friends and family to support our website. We wrote this article by researching on internet to find best content for you. You can find more articles like this on our website for free. We provided you some important tips regarding this topic by following them you can easily understand. If you need more information regarding Create Simple Login Application in Python Tutorial with Complete Source Code you can ask in comments or you can contact us via contact us form available on this website. Thanks for reading and sharing.


One Comment on “Create Simple Login Application in Python Tutorial with Complete Source Code”

Leave a Reply

Your email address will not be published. Required fields are marked *