How to Create Login Application in Python using SQLite Database?
Creating Login application in python programming language requires two major task. One is creating GUI and other is implementing logic.
Let’s talk about creating GUI:
Like in other programming language such as C#, You can create windows form application just by drag and dropping controls over the forms. So you may not need to write code to create GUI in C#. But here is python there is no such mechanism so you have to write code to create a GUI. There are various library through which you can create GUI in python. But we are going to use the builtin module that is tkinter. So let’s create the GUI.
We are going to use the intellij IDEA to create this project so download and install it.
Basic project setup STEPS:
- Install python in your system, download it from here
- Install python plugin in the intellij IDEA from file>setting>plugin and search python and install it.
- create virtual environment from file>project structure>SDKs and click on plus sign which you can see on the top of the middle part of the window and select add python SDK. New window pop up and there you can see the virtual env and click OK.
- create one project from file>new>project and give project name.
- now right click over your project>new>python file give file and and enter.
let’s write the GUI CODE:
STEPS:
[sociallocker]
1. import tkinter and messagebox from tkinter.
import tkinter from tkinter import messagebox
2. create window then give title and size of window.
main_window=tkinter.Tk() main_window.title('Login App') main_window.geometry('400x300')
3. define variable which we will associate with the GUI element to take input from the user.
user_input=tkinter.StringVar() pass_input=tkinter.StringVar() padd=20 main_window['padx']=padd
4. write code to design put label and textbox on the window.
info_label=tkinter.Label(main_window, text='Login Application') info_label.grid(row=0, column=0, pady=20) info_user=tkinter.Label(main_window, text='Username') info_user.grid(row=1, column=0) userinput=tkinter.Entry(main_window, textvariable=user_input) userinput.grid(row=1, column=1) info_pass=tkinter.Label(main_window, text='Password') info_pass.grid(row=2, column=0, pady=20) passinput=tkinter.Entry(main_window, textvariable=pass_input, show='*') passinput.grid(row=2, column=1) login_btn=tkinter.Button(main_window, text='Login', command=login) login_btn.grid(row=3, column=1)
4. write mainloop method so that our GUI works.
main_window.mainloop()
4. now import sqlite3 so that we can use sqlite in our code.
import sqlite3
5. Define one function and write the following code after defining this function you have one property called command of button and associate that property with this function without writing the parentheses i.e. () because we are not assigning the value return by the function but we are just calling the function by giving it’s name only so that this function will execute normally.[you can see doing this in above code don’t worry it’s for your knowledge]
def login(): db=sqlite3.connect('login.sqlite') db.execute('CREATE TABLE IF NOT EXISTS login(username TEXT, password TEXT)') # db.execute("INSERT INTO login(username, password) VALUES('admin', 'admin')") db.execute("INSERT INTO login(username, password) VALUES('user', 'admin')") cursor=db.cursor() cursor.execute("SELECT * FROM login where username=? AND password=?",(userinput.get(), pass_input.get())) row=cursor.fetchone() if row: messagebox.showinfo('info', 'login success') else: messagebox.showinfo('info', 'login failed') cursor.connection.commit() db.close()
6. Now right click and run the file. That’s all.
[/sociallocker]
if you have any confusion please watch the following video.