How to Build a Contact Management System in Python Using Tkinter and SQLite. In today’s digital age, efficiently managing contacts is essential for both personal and professional success.
Whether you’re a developer looking to organize your contacts or a business aiming to streamline communication, creating a custom contact management system can be a powerful solution.
In this tutorial, we will guide you through building a contact management system in Python using Tkinter for the graphical user interface (GUI) and SQLite for database management.
By the end of this guide, you’ll have a fully functional application that allows you to add, view, search, update, and delete contacts with ease, all within a user-friendly interface.
How to Build a Contact Management System in Python Using Tkinter and SQLite – Creating a contact management GUI in Python can be achieved using libraries like Tkinter for the GUI and SQLite for database management. Below is an example of how you can implement a contact management system with the features you specified:
import tkinter as tk
from tkinter import messagebox, simpledialog
import sqlite3
import re
# Function to validate phone numbers
def is_valid_phone_number(phone):
pattern = re.compile(r'^\+?[1-9]\d{1,14}$')
return pattern.match(phone)
# Function to create the database and table if not exists
def create_db():
conn = sqlite3.connect('contacts.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS contacts (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
number TEXT NOT NULL UNIQUE,
email TEXT
)
''')
conn.commit()
conn.close()
# Function to add a new contact
def add_contact(name, number, email):
if not is_valid_phone_number(number):
messagebox.showerror("Invalid Phone Number", "The phone number format is incorrect.")
return
conn = sqlite3.connect('contacts.db')
cursor = conn.cursor()
try:
cursor.execute('INSERT INTO contacts (name, number, email) VALUES (?, ?, ?)', (name, number, email))
conn.commit()
messagebox.showinfo("Success", "Contact added successfully!")
except sqlite3.IntegrityError:
messagebox.showerror("Error", "A contact with this number already exists.")
finally:
conn.close()
# Function to display all contacts
def display_contacts():
conn = sqlite3.connect('contacts.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM contacts')
rows = cursor.fetchall()
conn.close()
display_window = tk.Toplevel(root)
display_window.title("All Contacts")
for row in rows:
tk.Label(display_window, text=f"ID: {row[0]}, Name: {row[1]}, Number: {row[2]}, Email: {row[3]}").pack()
# Function to search for a contact
def search_contact():
search_term = simpledialog.askstring("Search", "Enter name or number:")
conn = sqlite3.connect('contacts.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM contacts WHERE name LIKE ? OR number LIKE ?', (f'%{search_term}%', f'%{search_term}%'))
rows = cursor.fetchall()
conn.close()
search_window = tk.Toplevel(root)
search_window.title("Search Results")
for row in rows:
tk.Label(search_window, text=f"ID: {row[0]}, Name: {row[1]}, Number: {row[2]}, Email: {row[3]}").pack()
# Function to update a contact
def update_contact():
contact_id = simpledialog.askinteger("Update", "Enter the ID of the contact to update:")
new_name = simpledialog.askstring("Update", "Enter new name:")
new_number = simpledialog.askstring("Update", "Enter new number:")
new_email = simpledialog.askstring("Update", "Enter new email:")
if not is_valid_phone_number(new_number):
messagebox.showerror("Invalid Phone Number", "The phone number format is incorrect.")
return
conn = sqlite3.connect('contacts.db')
cursor = conn.cursor()
cursor.execute('UPDATE contacts SET name = ?, number = ?, email = ? WHERE id = ?', (new_name, new_number, new_email, contact_id))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Contact updated successfully!")
# Function to delete a contact
def delete_contact():
contact_id = simpledialog.askinteger("Delete", "Enter the ID of the contact to delete:")
conn = sqlite3.connect('contacts.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM contacts WHERE id = ?', (contact_id,))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Contact deleted successfully!")
# GUI setup
root = tk.Tk()
root.title("Contact Management System")
create_db()
tk.Button(root, text="Add Contact", command=lambda: add_contact(
simpledialog.askstring("Add Contact", "Enter name:"),
simpledialog.askstring("Add Contact", "Enter number:"),
simpledialog.askstring("Add Contact", "Enter email:")
)).pack()
tk.Button(root, text="Display Contacts", command=display_contacts).pack()
tk.Button(root, text="Search Contact", command=search_contact).pack()
tk.Button(root, text="Update Contact", command=update_contact).pack()
tk.Button(root, text="Delete Contact", command=delete_contact).pack()
root.mainloop()
Output:

Explanation:
- Database Setup: The
create_dbfunction initializes an SQLite database and creates a table for storing contacts. - Adding Contacts: The
add_contactfunction inserts a new contact into the database, with validation for the phone number format. - Displaying Contacts: The
display_contactsfunction retrieves and shows all contacts. - Searching Contacts: The
search_contactfunction allows searching by name or number. - Updating Contacts: The
update_contactfunction updates an existing contact based on the ID. - Deleting Contacts: The
delete_contactfunction removes a contact by ID.
You’ll need to install Tkinter if it’s not already installed. For SQLite, Python’s standard library includes it, so no additional installation is required.
Feel free to modify and extend this code as needed!
In the provided code, contacts are stored in an SQLite database (contacts.db), and they can be viewed using the “Display Contacts” button in the GUI.
Steps to View Added Contacts:
- Run the Application: After running the script, the main window of the contact management system will appear.
- Add a Contact: Click the “Add Contact” button and input the contact’s name, number, and email. Once you click “OK,” the contact will be saved to the database.
- Display Contacts: To view all the contacts you’ve added, click the “Display Contacts” button in the main window. This will open a new window displaying all contacts stored in the database.
This display window will show the contact’s ID, name, number, and email, allowing you to see the details of all contacts added so far.
Building a contact management system in Python using Tkinter and SQLite provides a comprehensive introduction to GUI application development and database management. Throughout this tutorial, we explored the fundamental steps needed to create a fully functional application, from setting up the graphical user interface with Tkinter to managing data storage and retrieval with SQLite.
By implementing features such as adding, displaying, searching, updating, and deleting contacts, you’ve gained hands-on experience in handling user inputs, validating data, and interacting with a database. You also learned how to ensure data integrity with input validation and how to provide a user-friendly experience through a well-structured GUI.
This project serves as a solid foundation for more complex applications. You can further enhance this contact management system by adding features like importing/exporting contacts to and from CSV files, integrating cloud-based storage, or implementing user authentication for added security. Additionally, you can refine the user interface by incorporating more advanced Tkinter widgets or styling elements.
As you continue your journey in Python development, this project highlights the power and flexibility of Python in building practical, real-world applications. Whether you’re looking to expand this project or create entirely new applications, the skills and concepts learned here will be invaluable.





Leave a Reply