Site icon TechVidvan

Create Contact Book in Python

python project contact book

In this Python project, we will build a GUI-based Contact Book using the Tkinter and sqlite libraries and the messagebox module. It is an advanced level project, and you will learn so many things that you will be able to apply in real life. Let’s get started!😊

About Contact Books:

Do you remember those books where your parents used to store all their contacts’ names, phone numbers, addresses, and even where they remember them from? Oh, those paper-y days.

Today, we are going to create the same thing, except this one will be on your computer, with the data corresponding to the data from a database where you will store it.

About the project:

The objective of this is to create a GUI based Contact Book. To build this, you will need intermediate understanding of Tkinter and SQLite libraries, and only a basic understanding of the messagebox module. You will also need to have knowledge about the SQL commands: SELECT, INSERT, CREATE TABLE and DELETE.

Project Prerequisites:

To build this project, we will need the following libraries:
1. Tkinter – To create the GUI
2. SQLite (sqlite) – To connect to a SQL database and perform operations on the contacts.

The Tkinter library comes pre-installed with Python, however the SQLite library does not. To install it, you will need to run the following command on your terminal.

python -m pip install sqlite3

We need to “3” after sqlite to specify that we need the 3.x version (newer) of the sqlite library that corresponds with Python 3.x

Download Python Contact Book Project

Download source code of python contact book: Python Contact Book Project Code

Project File Structure:

Here are the steps you will need to execute to build python address book project:

1. Connecting to the database and initializing it.
2. Initializing the GUI window, placing the frames and header label and creating some commonly used variables [of this project].
3. Placing the components in the three frames.
4. Creating the functions for the buttons in the frames.

Let’s take a closer look at these steps:

1. Importing the necessary libraries and modules:

from tkinter import *
import tkinter.messagebox as mb
import sqlite3

2. Connecting to the database and initializing it:

# Connecting and Initializing the Database where we will store all the data
connector = sqlite3.connect('contacts.db')
cursor = connector.cursor()

cursor.execute(
"CREATE TABLE IF NOT EXISTS CONTACT_BOOK (S_NO INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME TEXT, EMAIL TEXT, PHONE_NUMBER TEXT, ADDRESS TEXT)"
)

Explanation:

"CREATE TABLE IF NOT EXISTS {Table Name} ({COLUMN1} {TYPE}, {COLUMN2} {TYPE} ....)"

3. Initializing the GUI window, placing the frames and header label and creating some commonly used variables:

# Initializing the GUI window
root = Tk()
root.title("TechVidvan Contact Book")
root.geometry('700x550')
root.resizable(0, 0)

# Creating the color and font variables
lf_bg = 'Gray70'  # Lightest Shade
cf_bg = 'Gray57'
rf_bg = 'Gray35'  # Darkest Shade
frame_font = ("Garamond", 14)

# Creating the StringVar variables
name_strvar = StringVar()
phone_strvar = StringVar()
email_strvar = StringVar()

# Creating and placing the components in the window
Label(root, text='CONTACT BOOK', font=("Noto Sans CJK TC", 15, "bold"), bg='Black', fg='White').pack(side=TOP, fill=X)


# Frame on the left
left_frame = Frame(root, bg=lf_bg) 
left_frame.place(relx=0, relheight=1, y=30, relwidth=0.3)


# Frame in the center
center_frame = Frame(root, bg=cf_bg)
center_frame.place(relx=0.3, relheight=1, y=30, relwidth=0.3)


# Frame on the right
right_frame = Frame(root, bg=rf_bg)
right_frame.place(relx=0.6, relwidth=0.4, relheight=1, y=30)

Explanation:

4. Placing the components in the three frames:

# Placing components in the left frame
Label(left_frame, text='Name', bg=lf_bg, font=frame_font).place(relx=0.3, rely=0.05)

name_entry = Entry(left_frame, width=15, font=("Verdana", 11), textvariable=name_strvar)
name_entry.place(relx=0.1, rely=0.1)

Label(left_frame, text='Phone no.', bg=lf_bg, font=frame_font).place(relx=0.23, rely=0.2)

phone_entry = Entry(left_frame, width=15, font=("Verdana", 11), textvariable=phone_strvar)
phone_entry.place(relx=0.1, rely=0.25)

Label(left_frame, text='Email', bg=lf_bg, font=frame_font).place(relx=0.3, rely=0.35)

email_entry = Entry(left_frame, width=15, font=("Verdana", 11), textvariable=email_strvar)
email_entry.place(relx=0.1, rely=0.4)

Label(left_frame, text='Address', bg=lf_bg, font=frame_font).place(relx=0.28, rely=0.5)

address_entry = Text(left_frame, width=15, font=("Verdana", 11), height=5)
address_entry.place(relx=0.1, rely=0.55)

# Placing components in the Middle Frame
search_entry = Entry(center_frame, width=18, font=("Verdana", 12), textvariable=search_strvar).place(relx=0.08, rely=0.04)

Button(center_frame, text='Search', font=frame_font, width=15, command=search).place(relx=0.13, rely=0.1)
Button(center_frame, text='Add Record', font=frame_font, width=15, command=submit_record).place(relx=0.13, rely=0.2)
Button(center_frame, text='View Record', font=frame_font, width=15, command=view_record).place(relx=0.13, rely=0.3)
Button(center_frame, text='Clear Fields', font=frame_font, width=15, command=clear_fields).place(relx=0.13, rely=0.4)
Button(center_frame, text='Delete Record', font=frame_font, width=15, command=delete_record).place(relx=0.13, rely=0.5)
Button(center_frame, text='Delete All Records', font=frame_font, width=15, command=delete_all_records).place(relx=0.13, rely=0.6)

# Placing components in the Right Frame
Label(right_frame, text='Saved Contacts', font=("Noto Sans CJK TC", 14), bg=rf_bg).place(relx=0.25, rely=0.05)

listbox = Listbox(right_frame, selectbackground='SkyBlue', bg='Gainsboro', font=('Helvetica', 12), height=20, width=25)
scroller = Scrollbar(listbox, orient=VERTICAL, command=listbox.yview)
scroller.place(relx=0.93, rely=0, relheight=1)
listbox.config(yscrollcommand=scroller.set)
listbox.place(relx=0.1, rely=0.15)

list_contacts()

Explanation:

5. Creating the functions for the buttons in the frames:

# Defining the functions
def submit_record():
    global name_strvar, email_strvar, phone_strvar, address_entry
    global cursor
    name, email, phone, address = name_strvar.get(), email_strvar.get(), phone_strvar.get(), address_entry.get(1.0, END)

    if name=='' or email=='' or phone=='' or address=='':
        mb.showerror('Error!', "Please fill all the fields!")
    else:
        cursor.execute(
        "INSERT INTO CONTACT_BOOK (NAME, EMAIL, PHONE_NUMBER, ADDRESS) VALUES (?,?,?,?)", (name, email, phone, address))
        connector.commit()
        mb.showinfo('Contact added', 'We have stored the contact successfully!')
        listbox.delete(0, END)
        list_contacts()
        clear_fields()


def list_contacts():
    curr = connector.execute('SELECT NAME FROM CONTACT_BOOK')
    fetch = curr.fetchall()

    for data in fetch:
        listbox.insert(END, data)


def delete_record():
    global listbox, connector, cursor

    if not listbox.get(ACTIVE):
        mb.showerror("No item selected", "You have not selected any item!")

    cursor.execute('DELETE FROM CONTACT_BOOK WHERE NAME=?', (listbox.get(ACTIVE)))
    connector.commit()

    mb.showinfo('Contact deleted', 'The desired contact has been deleted')
    listbox.delete(0, END)
    list_contacts()


def delete_all_records():
    cursor.execute('DELETE FROM CONTACT_BOOK')
    connector.commit()

    mb.showinfo("All records deleted", "All the records in your contact book have been deleted")

    listbox.delete(0, END)
    list_contacts()


def view_record():
    global name_strvar, phone_strvar, email_strvar, address_entry, listbox

    curr = cursor.execute('SELECT * FROM CONTACT_BOOK WHERE NAME=?', listbox.get(ACTIVE))
    values = curr.fetchall()[0]

    name_strvar.set(values[1]); phone_strvar.set(values[3]); email_strvar.set(values[2])

    address_entry.delete(1.0, END)
    address_entry.insert(END, values[4])


def clear_fields():
    global name_strvar, phone_strvar, email_strvar, address_entry, listbox

    listbox.selection_clear(0, END)

    name_strvar.set('')
    phone_strvar.set('')
    email_strvar.set('')
    address_entry.delete(1.0, END)

def search():
   query = str(search_strvar.get())

   if query != '':
       listbox.delete(0, END)

       curr = connector.execute('SELECT * FROM CONTACT_BOOK WHERE NAME LIKE ?', ('%'+query+'%', ))
       check = curr.fetchall()

      for data in check:
        listbox.insert(END, data[1])

Explanation:

Python Address Book Output

Summary

Congratulations! You have now created your own Contact Book using the Tkinter and SQLite libraries. Now you can have a copy of all the contact details of a person. You can use it in case you need to send them a postal mail, electronic mail or even a message.

Exit mobile version