This tutorial uses Python to create a login and registration form. Python is an advanced programming language that is widely used as common practice for developers. Beginners will find Python’s cleaner syntax and indentation structure and fewer semicolon problems to be easier to learn. Let the coding do the work.
Getting started:
First, you need to download and install Python IDLE. Here are links to Python integrated development and learning environments:
Installing SQLite Browser
After installing Python, install SQLite. Here’s a link to SQLite’s dB browser: Then open SQLite and create a database named “db_member”.
Importing Modules
After setting up the installation and database, run IDLE, click File, then New File. After that, a new window will appear with an empty file. This is a text editor for Python.
Then copy and paste the code below into the IDLE text editor.
from tkinter import *
import tkinter.messagebox as tkMessageBox
import sqlite3
Setting up the Main Frame
After importing the module, create the mainframe for your application. To do this, simply copy and paste the code below into the IDLE text editor.
root = Tk()
root.title("Python: Simple Inventory System")
width = 640
height = 480
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 Database Connection
Now that we have our design set up, let’s create a database function. To do this, simply copy and paste the code below into the IDLE text editor.
def Database():
global conn, cursor
conn = sqlite3.connect("db_member.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT, firstname TEXT, lastname TEXT)")
Assigning Variables
Assign variables here. This code assigns each variable that will be used later in the application.
#=======================================VARIABLES=====================================
USERNAME = StringVar()
PASSWORD = StringVar()
FIRSTNAME = StringVar()
LASTNAME = StringVar()
Designing the Layout
Now that we’ve created the main frame, let’s add a layout to our application. Just copy and paste the code below into the IDLE text editor.
def LoginForm():
global LoginFrame, lbl_result1
LoginFrame = Frame(root)
LoginFrame.pack(side=TOP, pady=80)
lbl_username = Label(LoginFrame, text="Username:", font=('arial', 25), bd=18)
lbl_username.grid(row=1)
lbl_password = Label(LoginFrame, text="Password:", font=('arial', 25), bd=18)
lbl_password.grid(row=2)
lbl_result1 = Label(LoginFrame, text="", font=('arial', 18))
lbl_result1.grid(row=3, columnspan=2)
username = Entry(LoginFrame, font=('arial', 20), textvariable=USERNAME, width=15)
username.grid(row=1, column=1)
password = Entry(LoginFrame, font=('arial', 20), textvariable=PASSWORD, width=15, show="*")
password.grid(row=2, column=1)
btn_login = Button(LoginFrame, text="Login", font=('arial', 18), width=35, command=Login)
btn_login.grid(row=4, columnspan=2, pady=20)
lbl_register = Label(LoginFrame, text="Register", fg="Blue", font=('arial', 12))
lbl_register.grid(row=0, sticky=W)
lbl_register.bind('<Button-1>', ToggleToRegister)
def RegisterForm():
global RegisterFrame, lbl_result2
RegisterFrame = Frame(root)
RegisterFrame.pack(side=TOP, pady=40)
lbl_username = Label(RegisterFrame, text="Username:", font=('arial', 18), bd=18)
lbl_username.grid(row=1)
lbl_password = Label(RegisterFrame, text="Password:", font=('arial', 18), bd=18)
lbl_password.grid(row=2)
lbl_firstname = Label(RegisterFrame, text="Firstname:", font=('arial', 18), bd=18)
lbl_firstname.grid(row=3)
lbl_lastname = Label(RegisterFrame, text="Lastname:", font=('arial', 18), bd=18)
lbl_lastname.grid(row=4)
lbl_result2 = Label(RegisterFrame, text="", font=('arial', 18))
lbl_result2.grid(row=5, columnspan=2)
username = Entry(RegisterFrame, font=('arial', 20), textvariable=USERNAME, width=15)
username.grid(row=1, column=1)
password = Entry(RegisterFrame, font=('arial', 20), textvariable=PASSWORD, width=15, show="*")
password.grid(row=2, column=1)
firstname = Entry(RegisterFrame, font=('arial', 20), textvariable=FIRSTNAME, width=15)
firstname.grid(row=3, column=1)
lastname = Entry(RegisterFrame, font=('arial', 20), textvariable=LASTNAME, width=15)
lastname.grid(row=4, column=1)
btn_login = Button(RegisterFrame, text="Register", font=('arial', 18), width=35, command=Register)
btn_login.grid(row=6, columnspan=2, pady=20)
lbl_login = Label(RegisterFrame, text="Login", fg="Blue", font=('arial', 12))
lbl_login.grid(row=0, sticky=W)
lbl_login.bind('<Button-1>', ToggleToLogin)
Creating the Main Function
Here is the code with the main functionality: This code will register the user and allow them to log in after creating an account. To do this, just copy and write these code blocks.
#=======================================METHODS=======================================
def Exit():
result = tkMessageBox.askquestion('System', 'Are you sure you want to exit?', icon="warning")
if result == 'yes':
root.destroy()
exit()
def ToggleToLogin(event=None):
RegisterFrame.destroy()
LoginForm()
def ToggleToRegister(event=None):
LoginFrame.destroy()
RegisterForm()
def Register():
Database()
if USERNAME.get == "" or PASSWORD.get() == "" or FIRSTNAME.get() == "" or LASTNAME.get == "":
lbl_result2.config(text="Please complete the required field!", fg="orange")
else:
cursor.execute("SELECT * FROM `member` WHERE `username` = ?", (USERNAME.get(),))
if cursor.fetchone() is not None:
lbl_result2.config(text="Username is already taken", fg="red")
else:
cursor.execute("INSERT INTO `member` (username, password, firstname, lastname) VALUES(?, ?, ?, ?)", (str(USERNAME.get()), str(PASSWORD.get()), str(FIRSTNAME.get()), str(LASTNAME.get())))
conn.commit()
USERNAME.set("")
PASSWORD.set("")
FIRSTNAME.set("")
LASTNAME.set("")
lbl_result2.config(text="Successfully Created!", fg="black")
cursor.close()
conn.close()
def Login():
Database()
if USERNAME.get == "" or PASSWORD.get() == "":
lbl_result1.config(text="Please complete the required field!", fg="orange")
else:
cursor.execute("SELECT * FROM `member` WHERE `username` = ? and `password` = ?", (USERNAME.get(), PASSWORD.get()))
if cursor.fetchone() is not None:
lbl_result1.config(text="You Successfully Login", fg="blue")
else:
lbl_result1.config(text="Invalid Username or password", fg="red")
LoginForm()
Additional Layout
The code below is for creating an exit action in the menu bar.
#========================================MENUBAR WIDGETS==================================
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=Exit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
Initializing the Application
Save the program as index.py after the function is complete. The code will be run during this process in order to ensure that the main has been initialized properly. To achieve that, copy the code below, and then paste it into the IDLE text editor.
#========================================INITIALIZATION===================================
if __name__ == '__main__':
root.mainloop()