import turtle from tkinter.simpledialog import askstring from tkinter.messagebox import showinfo contactList = [] #Why is this variable Global? turtle.setup(480,360) #Images addContactImage = "addContact.gif" turtle.register_shape(addContactImage) deleteContactImage = "deleteContact.gif" turtle.register_shape(deleteContactImage) findContactImage = "findContact.gif" turtle.register_shape(findContactImage) clearListImage = "clearList.gif" turtle.register_shape(clearListImage) contactListBackground = "contactList.gif" #Button Turtles addContact = turtle.Turtle() addContact.shape(addContactImage) deleteContact = turtle.Turtle() deleteContact.shape(deleteContactImage) findContact = turtle.Turtle() findContact.shape(findContactImage) clearList = turtle.Turtle() clearList.shape(clearListImage) #set background image screen = turtle.Screen() screen.bgpic(contactListBackground) def setup(): turtle.tracer(0,0) addContact.penup() addContact.goto(150,100) deleteContact.penup() deleteContact.goto(150,50) findContact.penup() findContact.goto(150,0) clearList.penup() clearList.goto(150,-50) turtle.update() addContact.onclick(addContactHandler) deleteContact.onclick(deleteContactHandler) findContact.onclick(findContactHandler) clearList.onclick(clearListHandler) ######################################################## #Contact ADT def contact(name, address, phone): return [name, address, phone] def contactName(contact): return contact[0] def contactAddress(contact): return contact[1] def contactPhone(contact): return contact[2] def contactDisplay(contact): return "Name: " + contactName(contact) + "\nAddress: " + contactAddress(contact) + "\nPhone: " + contactPhone(contact) #Button Handlers def addContactHandler(x,y): name = askstring("Name", "Name: ") address = askstring("Address", "Address: ") phone = askstring("Phone", "Phone: ") contactList.append(contact(name, address, phone)) saveFile("contactlist.txt") screen.listen() def deleteContactHandler(x,y): name = askstring("Remove","Contact to delete") for contact in contactList: if name in contactName(contact): showinfo("Removed", contactDisplay(contact)) contactList.remove(contact) saveFile("contactlist.txt") screen.listen() def findContactHandler(x,y): go = askstring("Find", "Would you like to search by [n]ame, [a]ddress or [p]hone") if go == "n": findByName() elif go == "a": findByAddress() elif go == "p": findByPhone() else: showinfo("Error", "Please type only 'n' 'a' or 'p'") screen.listen() def findByName(): name = askstring("Find","Name to search for") name2 = name.title() name3 = name.lower() for contact in contactList: if name2 in contactName(contact): showinfo("match", contactDisplay(contact)) for contact in contactList: if name3 in contactName(contact): showinfo("match", contactDisplay(contact)) def findByAddress(): address = askstring("Find","Address to search for") address2 = address.title() address3 = address.lower() for contact in contactList: if address2 in contactAddress(contact): showinfo("match", contactDisplay(contact)) for contact in contactList: if address3 in contactAddress(contact): showinfo("match", contactDisplay(contact)) def findByPhone(): phone = askstring("Find","Phone to search for") for contact in contactList: if phone in contactPhone(contact): showinfo("match", contactDisplay(contact)) def clearListHandler(x,y): sure = askstring("Clear", "Are you sure you want to clear the whole list [y/n]") if sure == "y": for contact in contactList: contactList.clear() else: return saveFile("contactlist.txt") screen.listen() def saveFile(filename): file = open(filename, "w") for contact in contactList: file.write(contactName(contact) + "|" + contactAddress(contact) + "|" + contactPhone(contact) + "\n") def displayContactList(): for contact in contactList: showinfo("Contact", contactDisplay(contact)) def loadFile(filename): file = open(filename, "r") for line in file: contact = line.strip("\n").split("|") contactList.append(contact) file.close() #MAIN SCRIPT setup() loadFile("contactlist.txt") screen.listen() screen.onkey(displayContactList, "space") #tests turtle.mainloop()