In this tutorial we will create a Simple Login Application in Python. Python has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. So let's now do the coding. Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/. Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
Creating the Database Connection
Then after setting up the design we will now create the database function. To do that just simply copy the code below and paste it inside the IDLE text editor
Creating the Main Function
This is the main function where the Entry will be check if there is a user exist in the database, after login correctly a new window will pop up. To do that just simply copy the code below then paste it inside the IDLE text editor
Initializing the Application
After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
There you have it we just created a Simple Login Application using Python. I hope that this simple tutorial help you expand the idea about Python programming. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/. Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
from tkinter import * import sqlite3
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
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)
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside 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)
Then after setting up the design we will now create the database function. To do that just simply copy the code below and paste it inside the IDLE text editor
#==============================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()
This is the main function where the Entry will be check if there is a user exist in the database, after login correctly a new window will pop up. To do that just simply copy the code below then paste it inside the IDLE text editor
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()
After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
#==============================INITIALIATION================================== if __name__ == '__main__': root.mainloop()
If you buy this mint (i.e source code) now, you will get lifetime mint updates for free.
What We offer:
Get final year project research topics. Browse free project topics and research material for final year students and researchers on Codemint. Start now.Talk to us right now: (+234)906-451-7926 (Call/WhatsApp)