So, I wrote an address book and I'm nearly done, but Im having a little trouble using the picle module to back up the input data to a file for later retrieval. Here is what I have so far: (Im working on a solution while I type this)

from tkinter import *
import pickle

def whichSelected () :
    print ("At %s of %d" % (select.curselection(), len(phonelist)))
    return int(select.curselection()[0])

def addEntry () :
    phonelist.append ([nameVar.get(), phoneVar.get(), emailVar.get()])
    setSelect ()

def updateEntry() :
    phonelist[whichSelected()] = [nameVar.get(), phoneVar.get(), emailVar.get()]
    setSelect ()

def deleteEntry() :
    del phonelist[whichSelected()]
    setSelect ()

def loadEntry  () :
    name, phone, email = phonelist[whichSelected()]
    nameVar.set(name)
    phoneVar.set(phone)
    emailVar.set(email) 

def makeWindow () :
    global nameVar, phoneVar, emailVar, select
    master = Tk()

    frame1 = Frame(master, height=144, width=144)
    frame1.pack()

    Label(frame1, text="Name").grid(row=0, column=0, sticky=W)
    nameVar = StringVar()
    name = Entry(frame1, textvariable=nameVar)
    name.grid(row=0, column=1, sticky=W)

    Label(frame1, text="Phone").grid(row=1, column=0, sticky=W)
    phoneVar= StringVar()
    phone= Entry(frame1, textvariable=phoneVar)
    phone.grid(row=1, column=1, sticky=W)

    Label(frame1, text="Email").grid(row=2, column=0, sticky=W)
    emailVar = StringVar()
    email= Entry(frame1, textvariable=emailVar)
    email.grid(row=2, column=1, sticky=W)

    frame2 = Frame(master, height=144, width=144)       # Row of buttons
    frame2.pack()
    b1 = Button(frame2,text=" Add  ",command=addEntry)
    b2 = Button(frame2,text="Update",command=updateEntry)
    b3 = Button(frame2,text="Delete",command=deleteEntry)
    b4 = Button(frame2,text=" Load ",command=loadEntry)
    b1.pack(side=LEFT); b2.pack(side=LEFT)
    b3.pack(side=LEFT); b4.pack(side=LEFT)

    frame3 = Frame(master, height=144, width=144)       # select of names
    frame3.pack()
    scroll = Scrollbar(frame3, orient=VERTICAL)
    select = Listbox(frame3, yscrollcommand=scroll.set, height=6)
    scroll.config (command=select.yview)
    scroll.pack(side=RIGHT, fill=Y)
    select.pack(side=LEFT,  fill=BOTH, expand=1)
    return master

def setSelect () :
    phonelist.sort()
    select.delete(0,END)
    for name, phone, email in phonelist :
        select.insert (END, name)

master = makeWindow()
setSelect ()
master.mainloop()


addressbookfile = 'addressbook.data'
addressbook = phonelist 
# write to storage 
f = open(addressbookfile, 'wb') 
pickle.dump(addressbook, f)
f.close()

# 'r'ead back from storage
f = open(addressbookfile, 'rb')
storedlist= pickle.load(f)
print(storedlist)

Recommended Answers

All 3 Replies

declare phonelist as a list as it should be used as a global varaiable

phonelist=[]

Load the data if phonelist is empty and data file exists. Something like this ...

from tkinter import *
import pickle
import os

def save_phonelist(addressbookfile, phonelist):
    addressbook = phonelist
    # write to storage
    f = open(addressbookfile, 'wb')
    pickle.dump(addressbook, f)
    f.close()

def load_phonelist(addressbookfile):
    # 'r'ead back from storage
    f = open(addressbookfile, 'rb')
    storedlist= pickle.load(f)
    print(storedlist)  # test
    f.close()
    return storedlist

'''
your updated code goes here

'''

def setSelect(phonelist) :
    phonelist.sort()
    select.delete(0,END)
    for name, phone, email in phonelist:
        select.insert(END, name)


addressbookfile = 'addressbook.data'
phonelist = []
if not phonelist and os.path.exists('addressbook.data'):
    phonelist = load_phonelist(addressbookfile)

master = makeWindow()
setSelect(phonelist)
master.mainloop()

Also make sure you save the data before you exit the program.

Worked like a charm, thanks you guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.