-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact book.py
65 lines (51 loc) · 1.74 KB
/
contact book.py
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
from tkinter import*
from tkinter import messagebox
def add_contact():
name = name_entry.get()
phone = phone_entry.get()
if name and phone:
contact_list.insert(END, f"{name} - {phone}")
name_entry.delete(0, END)
phone_entry.delete(0, END)
else:
messagebox.showerror("Error", "Please enter both name and phone number.")
def delete_contact():
try:
selected_index = contact_list.curselection()[0]
contact_list.delete(selected_index)
except IndexError:
messagebox.showerror("Error", "No contact selected.")
def clear_contacts():
contact_list.delete(0, END)
def get_selected_contact(event):
try:
selected_index = contact_list.curselection()[0]
selected_contact = contact_list.get(selected_index)
name, phone = selected_contact.split(" - ")
name_entry.delete(0, END)
phone_entry.delete(0, END)
name_entry.insert(END, name)
phone_entry.insert(END, phone)
except IndexError:
pass
root = Tk()
root.title("Contact Book")
# Create UI elements
name_label = Label(root, text="Name:")
name_label.pack()
name_entry = Entry(root)
name_entry.pack()
phone_label = Label(root, text="Phone:")
phone_label.pack()
phone_entry = Entry(root)
phone_entry.pack()
add_button = Button(root, text="Add Contact", command=add_contact)
add_button.pack()
delete_button = Button(root, text="Delete Contact", command=delete_contact)
delete_button.pack()
clear_button = Button(root, text="Clear Contacts", command=clear_contacts)
clear_button.pack()
contact_list = Listbox(root)
contact_list.pack()
contact_list.bind('<<ListboxSelect>>', get_selected_contact)
root.mainloop()