Skip to content

Instantly share code, notes, and snippets.

@arshamalh
Created January 15, 2022 09:12
Show Gist options
  • Save arshamalh/95baaee3cbdebdca169dbcb35600ef41 to your computer and use it in GitHub Desktop.
Save arshamalh/95baaee3cbdebdca169dbcb35600ef41 to your computer and use it in GitHub Desktop.
simple Tkinter contact book.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("480x170")
root.title("Raha Contacts books")
Name = StringVar()
Number = StringVar()
Address = StringVar()
SearchTerm = StringVar()
def getAllContacts():
with open("contacts.txt", "r") as f:
rows = f.readlines()
return [row.replace("\n", "").split(" : ") for row in rows]
def drawTable(window, contacts):
data = [["Name", "Phone", "Address"]]
data.extend(contacts)
total_rows = len(data)
total_columns = len(data[0])
for i in range(total_rows):
for j in range(total_columns):
e = Entry(window, width=15, font="arial 12")
e.grid(row=i, column=j)
e.insert(END, data[i][j])
def insertData():
name, phone, address = Name.get(), Number.get(), Address.get()
if name == "": messagebox.showwarning("Nothing to insert!", "Please specify a name to insert!")
else:
if phone == "": phone += " "
if address == "": address += " "
with open("contacts.txt", "a") as f:
f.write(name + " : " + phone + " : " + address + "\n")
messagebox.showinfo("Contact inserted", "Contact " + name + " inserted successfully!")
def searchData():
search_term = SearchTerm.get().strip().lower()
if search_term == "":
messagebox.showwarning("Nothing to search", "Please enter some data to search for!")
else:
listWindow([row for row in getAllContacts() if search_term in row[0].lower()])
def insertWindow():
newWindow = Toplevel(root)
newWindow.title("Insert new contact")
newWindow.geometry("400x250")
frame = Frame(newWindow)
frame.pack(pady=10)
frame1 = Frame(newWindow)
frame1.pack(pady=10)
frame2 = Frame(newWindow)
frame2.pack(pady=10)
frame3 = Frame(newWindow)
frame3.pack(pady=10)
Label(frame, text="Name: ", font="arial 12 bold").pack(side=LEFT)
Entry(frame, textvariable=Name, width=50).pack()
Label(frame1, text="Phone: ", font="arial 12 bold").pack(side=LEFT)
Entry(frame1, textvariable=Number, width=50).pack()
Label(frame2, text="Address: ", font="arial 12 bold").pack(side=LEFT)
Entry(frame2, textvariable=Address, width=50).pack()
Button(frame3, text="Submit", font="arial 12", command=insertData).pack(side=LEFT)
Button(frame3, text="Cancel", font="arial 12", command=newWindow.destroy).pack(
padx=10
)
def listWindow(contacts=None):
newWindow = Toplevel(root)
newWindow.title("List Window")
newWindow.geometry("550x300")
frame = Frame(newWindow)
frame.pack(pady=10)
if contacts is None:
print(">>>", getAllContacts())
drawTable(frame, getAllContacts())
else:
print(">>>", contacts)
drawTable(frame, contacts)
def searchWindow():
newWindow = Toplevel(root)
newWindow.title("New Window")
newWindow.geometry("400x120")
frame = Frame(newWindow)
frame.pack(pady=10)
frame_1 = Frame(newWindow)
frame_1.pack(pady=10)
Label(frame, text="Search term: ", font="arial 12 bold").pack(side=LEFT)
Entry(frame, textvariable=SearchTerm, width=50).pack()
Button(frame_1, text="Search", font="arial 12", command=searchData).pack(side=LEFT)
label = Label(root, text="Select your option")
label.pack(pady=10)
Button(root, text="Insert", font="arial 12", command=insertWindow).place(
x=50, y=50, width=100
)
Button(root, text="List", font="arial 12", command=listWindow).place(
x=175, y=50, width=100
)
Button(root, text="Search", font="arial 12", command=searchWindow).place(
x=300, y=50, width=100
)
Button(root, text="Exit", font="arial 12", command=root.destroy).place(
x=125, y=100, width=200
)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment