on
news
- Get link
- X
- Other Apps
This tutorial will guide you through creating a simple, text-based address book using Python. This project is perfect for beginners to learn fundamental programming concepts like dictionaries, loops, and user input. We'll break down the code step-by-step, explaining each part to ensure you understand how it works.
What is an Address Book?
An address book is a simple application that allows you to store and manage contact information, such as names and phone numbers. This version will be text-based, meaning it runs in the command prompt or terminal, and data is stored in memory while the program is running.
Prerequisites:
Let's Get Started!
Here's the Python code for our address book:
import tkinter as tk
from tkinter import messagebox
def add_contact():
"""Adds a new contact to the address book."""
name = name_entry.get()
phone = phone_entry.get()
if name and phone:
address_book[name] = phone
update_listbox()
name_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
else:
messagebox.showerror("Error", "Please enter both name and phone number.")
def view_contact():
"""Displays the selected contact's details."""
try:
selected_index = listbox.curselection()[0]
name = listbox.get(selected_index)
phone = address_book[name]
name_entry.delete(0, tk.END)
name_entry.insert(0, name)
phone_entry.delete(0, tk.END)
phone_entry.insert(0, phone)
except IndexError:
messagebox.showinfo("Info", "Please select a contact from the list.")
def search_contact():
"""Searches for a contact by name."""
search_name = search_entry.get()
if search_name in address_book:
messagebox.showinfo("Contact Found", f"Name: {search_name}, Phone: {address_book[search_name]}")
else:
messagebox.showinfo("Info", "Contact not found.")
def delete_contact():
"""Deletes a contact by name."""
try:
selected_index = listbox.curselection()[0]
name = listbox.get(selected_index)
del address_book[name]
update_listbox()
except IndexError:
messagebox.showinfo("Info", "Please select a contact to delete.")
def update_listbox():
"""Updates the listbox with the current contacts."""
listbox.delete(0, tk.END)
for name in address_book:
listbox.insert(tk.END, name)
# --- Main Window Setup ---
window = tk.Tk()
window.title("Address Book")
window.geometry("400x400")
# --- Address Book Data ---
address_book = {}
# --- Widgets ---
name_label = tk.Label(window, text="Name:")
name_label.grid(row=0, column=0, padx=5, pady=5)
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1, padx=5, pady=5)
phone_label = tk.Label(window, text="Phone:")
phone_label.grid(row=1, column=0, padx=5, pady=5)
phone_entry = tk.Entry(window)
phone_entry.grid(row=1, column=1, padx=5, pady=5)
add_button = tk.Button(window, text="Add Contact", command=add_contact)
add_button.grid(row=2, column=0, columnspan=2, padx=5, pady=5)
view_button = tk.Button(window, text="View Contact", command=view_contact)
view_button.grid(row=3, column=0, padx=5, pady=5)
search_label = tk.Label(window, text="Search:")
search_label.grid(row=4, column=0, padx=5, pady=5)
search_entry = tk.Entry(window)
search_entry.grid(row=4, column=1, padx=5, pady=5)
search_button = tk.Button(window, text="Search", command=search_contact)
search_button.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
delete_button = tk.Button(window, text="Delete Contact", command=delete_contact)
delete_button.grid(row=6, column=0, columnspan=2, padx=5, pady=5)
listbox = tk.Listbox(window, width=40, height=10)
listbox.grid(row=7, column=0, columnspan=2, padx=5, pady=5)
# --- Start the GUI ---
window.mainloop()
Code Explanation:
How to Run the Code on Windows:
The address book menu will appear in the Command Prompt. Follow the instructions to add, view, search, and delete contacts.
Further Improvements:
This tutorial provides a solid foundation for building more complex applications with Python. Experiment with the code, add new features, and have fun learning!
Comments
Post a Comment