Creating a Simple Contact Manager App with Tkinter in Python: A Step-by-Step Guide

Creating a Simple Contact Manager App with Tkinter in Python: A Step-by-Step Guide

Creating a Simple Contact Manager App with Tkinter in Python: A Step-by-Step Guide
Creating a Simple Contact Manager App with Tkinter in Python: A Step-by-Step Guide

Introduction:

In this tutorial, we will create a simple contact manager app using the Tkinter library in Python. The app will allow users to add, view, edit, and delete contacts. The final product will be a basic, yet functional, app that can be used to manage contacts.

Setting up Tkinter:

To begin, we will need to import the Tkinter library and create a new Tkinter window. We will also import the messagebox module, which will be used for error messages and confirmation prompts.

Creating the GUI:

Next, we will create the GUI for our app. This will include a menu bar, a toolbar, and a list box to display the contacts. We will also add buttons for adding, editing, and deleting contacts.

Adding Contacts:

We will create a function that will be called when the “Add Contact” button is clicked. This function will open a new window where the user can enter the contact’s name, phone number, and email address. Once the user enters this information, it will be added to the list box.

Viewing Contacts:

We will create a function that will be called when a contact in the list box is double-clicked. This function will open a new window displaying the selected contact’s name, phone number, and email address.

Editing and Deleting Contacts:

We will create two functions, one for editing and one for deleting a contact. These functions will be called when the corresponding button is clicked and the user has selected a contact from the list box. The editing function will open a new window with the contact’s information pre-filled, allowing the user to make changes. The deleting function will ask the user to confirm the deletion, and then remove the selected contact from the list box.

See also  New : DEMONSTRASI KONTEKSTUAL MODUL 2.2 PEMBELJARAN SOSIAL EMOSIONAL

Create Full Code Step By Step

import tkinter as tk
from tkinter import messagebox

# Create the main window
root = tk.Tk()
root.title("Contact Manager")

# Create a list to store the contacts
contacts = []

# Create a function to add a contact


def add_contact():
    # Create a new window for adding a contact
    add_window = tk.Toplevel(root)
    add_window.title("Add Contact")

    # Create labels and entry fields for the contact's name, phone number, and email
    name_label = tk.Label(add_window, text="Name:")
    name_label.grid(row=0, column=0)
    name_entry = tk.Entry(add_window)
    name_entry.grid(row=0, column=1)

    phone_label = tk.Label(add_window, text="Phone:")
    phone_label.grid(row=1, column=0)
    phone_entry = tk.Entry(add_window)
    phone_entry.grid(row=1, column=1)

    email_label = tk.Label(add_window, text="Email:")
    email_label.grid(row=2, column=0)
    email_entry = tk.Entry(add_window)
    email_entry.grid(row=2, column=1)

    # Create a function to save the contact information
    def save_contact():
        name = name_entry.get()
        phone = phone_entry.get()
        email = email_entry.get()
        if name and phone and email:
            # Add the contact to the list
            contacts.append({'name': name, 'phone': phone, 'email': email})
            messagebox.showinfo("Success", "Contact added successfully!")
            add_window.destroy()
        else:
            messagebox.showerror("Error", "Please enter all the fields.")

    # Create a save button
    save_button = tk.Button(add_window, text="Save", command=save_contact)
    save_button.grid(row=3, column=1)

# Create a function to view a contact


def view_contact(index):
    # Create a new window to display the contact information
    view_window = tk.Toplevel(root)
    view_window.title("View Contact")

    # Get the contact information
    contact = contacts[index]

    # Create labels to display the contact's name, phone number, and email
    name_label = tk.Label(view_window, text="Name:")
    name_label.grid(row=0, column=0)
    name_data = tk.Label(view_window, text=contact['name'])
    name_data.grid(row=0, column=1)

    phone_label = tk.Label(view_window, text="Phone:")
    phone_label.grid(row=1, column=0)
    phone_data = tk.Label(view_window, text=contact['phone'])
    phone_data.grid(row=1, column=1)

    email_label = tk.Label(view_window, text="Email:")
    email_label.grid(row=2, column=0)
    email_data = tk.Label(view_window, text=contact['email'])
    email_data.grid(row=2, column=1)

# Create a function to delete a contact


def delete_contact(index):
    # Confirm the deletion
    if messagebox.askyesno("Confirm", "Are you sure you want to delete this contact?"):
        del contacts[index]
        messagebox.showinfo("Success", "Contact deleted successfully!")

# Create a function to display the contacts


def display_contacts():
    # Clear the listbox
    contact_list.delete(0, tk.END)

    # Add the contacts to the listbox
    for index, contact in enumerate(contacts):
        contact_list.insert(index, contact['name'])


# Create a frame for the list of contacts
contact_frame = tk.Frame(root)
contact_frame.pack()

# Create a scrollbar for the list of contacts
scrollbar = tk.Scrollbar(contact_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

# Create a listbox for the contacts
contact_list = tk.Listbox(contact_frame, yscrollcommand=scrollbar.set)
contact_list.pack(side=tk.LEFT, fill=tk.BOTH)

# Configure the scrollbar
scrollbar.config(command=contact_list.yview)

# Create a frame for the buttons
button_frame = tk.Frame(root)
button_frame.pack()

# Create a button to add a contact
add_button = tk.Button(button_frame, text="Add", command=add_contact)
add_button.pack(side=tk.LEFT)

# Create a button to view a contact
view_button = tk.Button(button_frame, text="View", command=lambda: view_contact(
    contact_list.curselection()[0]))
view_button.pack(side=tk.LEFT)

# Create a button to delete a contact
delete_button = tk.Button(button_frame, text="Delete",
                          command=lambda: delete_contact(contact_list.curselection()[0]))
delete_button.pack(side=tk.LEFT)

# Create a button to display the contacts
display_button = tk.Button(
    button_frame, text="Display", command=display_contacts)
display_button.pack(side=tk.LEFT)

# Run the main loop
root.mainloop()

Output Application

Tkinter, Python, Contact Manager App, GUI, Programming, Tutorial, Code snippets
Tkinter, Python, Contact Manager App, GUI, Programming, Tutorial, Code snippets

Conclusion:

In this tutorial, we have created a simple contact manager app using the Tkinter library in Python. This app allows users to add, view, edit, and delete contacts. While this app is basic, it serves as a good starting point for creating more advanced apps.

Print Friendly, PDF & Email
See also  Create simple Calorie Calculator app using tkinter with code and explanation

Author

Leave a Reply