-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactbook.py
More file actions
106 lines (86 loc) · 3.48 KB
/
contactbook.py
File metadata and controls
106 lines (86 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import tkinter as tk
from tkinter import messagebox
# Initialize contact list
contacts = {}
# Functions
def add_contact():
name = entry_name.get()
phone = entry_phone.get()
email = entry_email.get()
address = entry_address.get()
if name and phone:
contacts[name] = {'phone': phone, 'email': email, 'address': address}
messagebox.showinfo("Success", "Contact added successfully!")
clear_entries()
view_contacts()
else:
messagebox.showwarning("Input Error", "Name and Phone are required!")
def view_contacts():
listbox_contacts.delete(0, tk.END)
for name, details in contacts.items():
listbox_contacts.insert(tk.END, f"{name}: {details['phone']}")
def search_contact():
search_query = entry_search.get()
listbox_contacts.delete(0, tk.END)
for name, details in contacts.items():
if search_query.lower() in name.lower() or search_query in details['phone']:
listbox_contacts.insert(tk.END, f"{name}: {details['phone']}")
def update_contact():
selected_contact = listbox_contacts.get(tk.ACTIVE)
if selected_contact:
name = selected_contact.split(":")[0].strip()
if name in contacts:
contacts[name] = {'phone': entry_phone.get(), 'email': entry_email.get(), 'address': entry_address.get()}
messagebox.showinfo("Success", "Contact updated successfully!")
clear_entries()
view_contacts()
def delete_contact():
selected_contact = listbox_contacts.get(tk.ACTIVE)
if selected_contact:
name = selected_contact.split(":")[0].strip()
if name in contacts:
del contacts[name]
messagebox.showinfo("Success", "Contact deleted successfully!")
view_contacts()
def clear_entries():
entry_name.delete(0, tk.END)
entry_phone.delete(0, tk.END)
entry_email.delete(0, tk.END)
entry_address.delete(0, tk.END)
# GUI setup
root = tk.Tk()
root.title("Contact Manager")
# Labels and Entries
label_name = tk.Label(root, text="Name:")
label_name.grid(row=0, column=0)
entry_name = tk.Entry(root)
entry_name.grid(row=0, column=1, padx=5, pady=5)
label_phone = tk.Label(root, text="Phone:")
label_phone.grid(row=1, column=0)
entry_phone = tk.Entry(root)
entry_phone.grid(row=1, column=1, padx=5, pady=5)
label_email = tk.Label(root, text="Email:")
label_email.grid(row=2, column=0)
entry_email = tk.Entry(root)
entry_email.grid(row=2, column=1, padx=5, pady=5)
label_address = tk.Label(root, text="Address:")
label_address.grid(row=3, column=0)
entry_address = tk.Entry(root)
entry_address.grid(row=3, column=1, padx=5, pady=5)
# Buttons
btn_add = tk.Button(root, text="Add Contact", command=add_contact)
btn_add.grid(row=4, column=0, columnspan=2, pady=5)
btn_update = tk.Button(root, text="Update Contact", command=update_contact)
btn_update.grid(row=5, column=0, columnspan=2, pady=5)
btn_delete = tk.Button(root, text="Delete Contact", command=delete_contact)
btn_delete.grid(row=6, column=0, columnspan=2, pady=5)
# Contact Listbox
listbox_contacts = tk.Listbox(root, height=10, width=50)
listbox_contacts.grid(row=7, column=0, columnspan=2, pady=5)
# Search bar
entry_search = tk.Entry(root)
entry_search.grid(row=8, column=0, padx=5, pady=5)
btn_search = tk.Button(root, text="Search Contact", command=search_contact)
btn_search.grid(row=8, column=1, padx=5, pady=5)
# Start the GUI
root.mainloop()