0

I am attempting to create contact application in python. It is relatively simple, and I don't think it requires much explaining. I am attempting to use the pickle module to store a list of tuples which are the main format in which the actual contact information is stored. When the program starts, it imports the list phonelist from where it is saved in pickle. I set it up to handle the error in the case that this is the first time the program is being run on a machine and the phonelist has never been pickled on said machine before. I know very little about pickle. This is my first time using it. This issue I am running into is that, after running the program the first time, and pickling the list for the first time, I have not been able to make changes to the list. Every time I run the program, change the list, close the program - with the expectations that my changes be saved via the pickling of the list that occurs at the end of the program - and reopen it, I get the same list as the very first time that I pickled the list.

    from tkinter import *
    import pickle
    try:
        phonelist = pickle.load( open( "save.p", "rb" ) )
    except FileNotFoundError:
        print ("First Run Error")
        phonelist = [['name', 'phone no.', 'email']]
        pass

    def whichSelected () :
        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
        win = Tk()

        frame1 = Frame(win)
        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(win)       # 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(win)       # 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 win

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

    pickle.dump( phonelist, open( "save.p", "wb" ) )

    win = makeWindow()
    setSelect ()
    win.mainloop()
4
  • Your best bet would be to use a different format to store the data; pickle is awful.
    – Wooble
    Jan 16, 2014 at 15:46
  • 1
    Storage format has nothing to do with his problem though.
    – Silas Ray
    Jan 16, 2014 at 15:55
  • I was considering it. This is just the easiest way I have found so far so I was hoping I could make it work.
    – wjjd225
    Jan 16, 2014 at 15:56
  • And lest the obligatory "you should be using context managers for working with files" comment gets missed... you should be using context managers for working with files. :) Using the with open(...) as ... syntax will handle error conditions in opening the file, and closing the file when done, for you.
    – Silas Ray
    Jan 16, 2014 at 16:03

2 Answers 2

0

It looks to me like you are saving the phone list before running the program. You should probably move the save step to after mainloop() exits. If you are updating the list in mainloop(), you need to save it after it is updated. You might want to be even more immediate too, saving at the end of addEntry(), updateEntry(), and deleteEntry() so that your stored data doesn't go out of sync with what is in memory if the program exits irregularly.

0

Your code tries to read any existing "save.p" file into phonelist. If that fails because the file doesn't exist, it initializes phonelist. However, before entering the application's main loop, it writes the data in phonelist to the file (and never again as far as I can tell). This means the file will never contain anything other than the initialization data, which will be read in and written out over and over again.

You need to add some kind of Save command and/or only (re)write the file if something in it has changed when the app exits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.