-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact book.py
More file actions
111 lines (93 loc) · 3.76 KB
/
contact book.py
File metadata and controls
111 lines (93 loc) · 3.76 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
import tkinter as tk
from tkinter import messagebox, simpledialog
import json
import os
# File to store contacts
CONTACTS_FILE = "contacts.json"
# Load contacts from JSON file
def load_contacts():
if os.path.exists(CONTACTS_FILE):
with open(CONTACTS_FILE, "r") as file:
return json.load(file)
return {}
# Save contacts to JSON file
def save_contacts():
with open(CONTACTS_FILE, "w") as file:
json.dump(contacts, file, indent=4)
# Add a new contact
def add_contact():
name = simpledialog.askstring("Add Contact", "Enter Name:")
phone = simpledialog.askstring("Add Contact", "Enter Phone Number:")
email = simpledialog.askstring("Add Contact", "Enter Email (optional):")
address = simpledialog.askstring("Add Contact", "Enter Address (optional):")
if name and phone:
contacts[name] = {"Phone": phone, "Email": email, "Address": address}
save_contacts()
update_listbox()
messagebox.showinfo("Success", "Contact added successfully!")
else:
messagebox.showwarning("Warning", "Name and Phone Number are required!")
# View all contacts
def update_listbox():
contact_listbox.delete(0, tk.END)
for name, info in contacts.items():
contact_listbox.insert(tk.END, f"{name} - {info['Phone']}")
# Search contact
def search_contact():
query = simpledialog.askstring("Search Contact", "Enter Name or Phone:")
if query:
for name, info in contacts.items():
if query in name or query in info["Phone"]:
messagebox.showinfo("Contact Found", f"Name: {name}\nPhone: {info['Phone']}\nEmail: {info['Email']}\nAddress: {info['Address']}")
return
messagebox.showwarning("Not Found", "Contact not found!")
# Update contact
def update_contact():
name = simpledialog.askstring("Update Contact", "Enter Name of Contact to Update:")
if name in contacts:
phone = simpledialog.askstring("Update Contact", "Enter New Phone Number:", initialvalue=contacts[name]["Phone"])
email = simpledialog.askstring("Update Contact", "Enter New Email:", initialvalue=contacts[name]["Email"])
address = simpledialog.askstring("Update Contact", "Enter New Address:", initialvalue=contacts[name]["Address"])
contacts[name] = {"Phone": phone, "Email": email, "Address": address}
save_contacts()
update_listbox()
messagebox.showinfo("Success", "Contact updated successfully!")
else:
messagebox.showwarning("Not Found", "Contact not found!")
# Delete contact
def delete_contact():
name = simpledialog.askstring("Delete Contact", "Enter Name of Contact to Delete:")
if name in contacts:
del contacts[name]
save_contacts()
update_listbox()
messagebox.showinfo("Success", "Contact deleted successfully!")
else:
messagebox.showwarning("Not Found", "Contact not found!")
# GUI Setup
app = tk.Tk()
app.title("Contact Book")
app.geometry("400x500")
app.config(bg="#f5f5f5")
contacts = load_contacts()
# Title
title_label = tk.Label(app, text="Contact Book", font=("Arial", 16, "bold"), bg="#f5f5f5")
title_label.pack(pady=10)
# Contact Listbox
contact_listbox = tk.Listbox(app, width=50, height=15)
contact_listbox.pack(pady=10)
update_listbox()
# Buttons
btn_frame = tk.Frame(app, bg="#f5f5f5")
btn_frame.pack()
buttons = [
("Add Contact", add_contact),
("Search Contact", search_contact),
("Update Contact", update_contact),
("Delete Contact", delete_contact)
]
for text, command in buttons:
btn = tk.Button(btn_frame, text=text, command=command, width=20, bg="#4CAF50", fg="white")
btn.pack(pady=5)
# Run the application
app.mainloop()