-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactbook.py
More file actions
125 lines (106 loc) · 4.22 KB
/
Copy pathcontactbook.py
File metadata and controls
125 lines (106 loc) · 4.22 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import tkinter as tk
from tkinter import messagebox, simpledialog
contacts = []
# Function to refresh contact list display
def refresh_contact_list():
contact_listbox.delete(0, tk.END)
for contact in contacts:
contact_listbox.insert(tk.END, f"{contact['name']} - {contact['phone']}")
# Add new contact
def add_contact():
name = name_entry.get()
phone = phone_entry.get()
email = email_entry.get()
address = address_entry.get()
if name and phone:
contacts.append({'name': name, 'phone': phone, 'email': email, 'address': address})
refresh_contact_list()
clear_entries()
else:
messagebox.showwarning("Input Error", "Name and Phone are required!")
# Clear input fields
def clear_entries():
name_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
address_entry.delete(0, tk.END)
# Display contact details
def show_contact(event):
selection = contact_listbox.curselection()
if selection:
index = selection[0]
contact = contacts[index]
name_entry.delete(0, tk.END)
phone_entry.delete(0, tk.END)
email_entry.delete(0, tk.END)
address_entry.delete(0, tk.END)
name_entry.insert(0, contact['name'])
phone_entry.insert(0, contact['phone'])
email_entry.insert(0, contact['email'])
address_entry.insert(0, contact['address'])
# Delete selected contact
def delete_contact():
selection = contact_listbox.curselection()
if selection:
index = selection[0]
contacts.pop(index)
refresh_contact_list()
clear_entries()
else:
messagebox.showinfo("Delete Error", "Please select a contact to delete.")
# Update selected contact
def update_contact():
selection = contact_listbox.curselection()
if selection:
index = selection[0]
name = name_entry.get()
phone = phone_entry.get()
email = email_entry.get()
address = address_entry.get()
if name and phone:
contacts[index] = {'name': name, 'phone': phone, 'email': email, 'address': address}
refresh_contact_list()
clear_entries()
else:
messagebox.showwarning("Input Error", "Name and Phone are required!")
# Search contact by name or phone
def search_contact():
keyword = simpledialog.askstring("Search", "Enter name or phone:")
if keyword:
for i, contact in enumerate(contacts):
if keyword.lower() in contact['name'].lower() or keyword in contact['phone']:
contact_listbox.selection_clear(0, tk.END)
contact_listbox.selection_set(i)
contact_listbox.activate(i)
show_contact(None)
return
messagebox.showinfo("Not Found", "No matching contact found.")
# GUI Setup
root = tk.Tk()
root.title("Contact Book")
root.geometry("600x400")
root.resizable(False, False)
# Labels and entries
tk.Label(root, text="Name").grid(row=0, column=0, padx=10, pady=5)
name_entry = tk.Entry(root)
name_entry.grid(row=0, column=1, padx=10, pady=5)
tk.Label(root, text="Phone").grid(row=1, column=0, padx=10, pady=5)
phone_entry = tk.Entry(root)
phone_entry.grid(row=1, column=1, padx=10, pady=5)
tk.Label(root, text="Email").grid(row=2, column=0, padx=10, pady=5)
email_entry = tk.Entry(root)
email_entry.grid(row=2, column=1, padx=10, pady=5)
tk.Label(root, text="Address").grid(row=3, column=0, padx=10, pady=5)
address_entry = tk.Entry(root)
address_entry.grid(row=3, column=1, padx=10, pady=5)
# Buttons
tk.Button(root, text="Add", command=add_contact, bg="#4CAF50", fg="white", width=10).grid(row=4, column=0, pady=10)
tk.Button(root, text="Update", command=update_contact, bg="#2196F3", fg="white", width=10).grid(row=4, column=1, pady=10)
tk.Button(root, text="Delete", command=delete_contact, bg="#f44336", fg="white", width=10).grid(row=5, column=0, pady=5)
tk.Button(root, text="Search", command=search_contact, bg="#9C27B0", fg="white", width=10).grid(row=5, column=1, pady=5)
# Contact list display
contact_listbox = tk.Listbox(root, width=50)
contact_listbox.grid(row=0, column=2, rowspan=6, padx=20, pady=10)
contact_listbox.bind('<<ListboxSelect>>', show_contact)
# Start main loop
root.mainloop()