Question: Q Write an address book program in python by using TKInter. Please download TKInterAddressBook.txt program. There are two task in this assignment. 1 . Add

Q Write an address book program in python by using TKInter. Please download TKInterAddressBook.txt program. There are two task in this assignment.
1. Add entry fields for City, State and Zip in the above Address Book program.
2. Add logic for the Previous and Last button. When user click Previous button the program should display previous record and when click on Last button it should display last record. here the addressbook import pickle
import os.path
from tkinter import * # Import tkinter
import tkinter.messagebox
class Address:
def __init__(self, name, street):
self.name = name
self.street = street
class AddressBook:
def __init__(self):
window = Tk() # Create a window
window.title("AddressBook") # Set title
self.nameVar = StringVar()
self.streetVar = StringVar()
frame1= Frame(window)
frame1.pack()
Label(frame1, text="Name").grid(row=1,
column=1, sticky=W)
Entry(frame1, textvariable=self.nameVar,
width=40).grid(row=1, column=2)
frame2= Frame(window)
frame2.pack()
Label(frame2, text="Address").grid(row=1,
column=1, sticky=W)
Entry(frame2, textvariable=self.streetVar,
width=40).grid(row=1, column=2)
frame4= Frame(window)
frame4.pack()
Button(frame4, text="Add",
command=self.processAdd).grid(row=1, column=1)
btFirst = Button(frame4, text="First",
command=self.processFirst).grid(row=1, column=2)
btNext = Button(frame4, text="Next",
command=self.processNext).grid(row=1, column=3)
btPrevious = Button(frame4, text = "Previous", command =
self.processPrevious).grid(row =1, column =4)
btLast = Button(frame4, text = "Last",
command = self.processLast).grid(row =1, column =5)
self.addressList = self.loadAddress()
self.current =0
if len(self.addressList)>0:
self.setAddress()
window.mainloop() # Create an event loop
def saveAddress(self):
outputFile = open("address1.dat", "wb")
pickle.dump(self.addressList, outputFile)
tkinter.messagebox.showinfo(
"Address saved", "A new address is saved")
outputFile.close()
def loadAddress(self):
if not os.path.isfile("address1.dat"):
return [] # Return an empty list
try:
inputFile = open("address1.dat", "rb")
addressList = pickle.load(inputFile)
except EOFError:
addressList =[]
inputFile.close()
return addressList
def processAdd(self):
address = Address(self.nameVar.get(),
self.streetVar.get()
)
self.addressList.append(address)
self.saveAddress()
def processFirst(self):
self.current =0
self.setAddress()
def processNext(self):
if self.current < len(self.addressList)-1:
self.current +=1
self.setAddress()
def processPrevious(self):
pass # Left as exercise
def processLast(self):
pass # Left as exercise
def setAddress(self):
self.nameVar.set(self.addressList[self.current].name)
self.streetVar.set(self.addressList[self.current].street)
AddressBook() # Create GUI

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!