Python Forum
error creating new object after loading pickled objects from file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error creating new object after loading pickled objects from file
#1
Hi All,

I am brand new to Python and am having good success so far, but I am stuck. I created a class and a Tkinter form to accept input and add objects to the class and then save the objects to a list and save them out to a pickle file. I am able to then import them again so my list is persistent, but when I try to add another Thing object after I import the previous data I get a "TypeError: 'Thing' object is not callable" error. I'm not sure what is happening since I can add as many new objects and put them in a list as I want without error, and I can then save and then import the pickled list fine, but I get the error only when I try to add a new object after I import the pickled list. I am using 3.7.

The code below works fine until I unremark the section of code right below the class definition. This is the section of code that imports the pickled data from the file. My print statements show that the data is pickling and unpickling fine. I'm wondering of the problem has something to do with a mismatch between what I'm pickling and unpickling (a "Things" list of "Thing" objects) versus what I'm then adding (a "Thing" object) after I import the pickled data. I get the error at the "x = Thing(nameEntry.get(), descriptionEntry.get())" line.

from tkinter import *
import pickle

Things = []
print('this next line will show the empty list of Thing objects:')
print(Things)


class Thing(object):
    def __init__(self, name, description):
        self.name = name
        self.description = description
        Things.append(self)


"""
# open file and assign Thing objects in saved file to Things list
with open('Things.pkl', 'rb') as input:
    Things = pickle.load(input)

# print(Things)   # this should print all Things brought in from file
print('this next section should show the name and description for each Thing in the Things list of Thing'
      ' objects read from the file when this app is first run, before we add any new Things')
for Thing in Things:
    print(Thing.name, Thing.description)
"""


def lftclick(event):
    x = Thing(nameEntry.get(), descriptionEntry.get())
    print('this next line will show the Thing object we just created:')
    print(x)


MainWindow = Tk()
# create 2 labels, 2 entry, and a button widgets
nameLabel = Label(MainWindow, text="Name")
nameEntry = Entry(MainWindow)
descriptionLabel = Label(MainWindow, text="Description")
descriptionEntry = Entry(MainWindow)
addButton = Button(MainWindow, text="Save Thing", foreground="red")
# position the widgets in rows and columns
nameLabel.grid(row=1, sticky=E)
descriptionLabel.grid(row=2, sticky=E)
nameEntry.grid(row=1, column=1)
descriptionEntry.grid(row=2, column=1)
addButton.grid(row=3, columnspan=2)
# bind the left click event for the button to the lftclick event
addButton.bind("<Button-1>", lftclick)
MainWindow.mainloop()


with open('Things.pkl', 'wb') as output:
    pickle.dump(Things, output)
Reply


Messages In This Thread
error creating new object after loading pickled objects from file - by arogers - Feb-02-2019, 03:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Next/Prev file without loading all filenames WilliamKappler 9 590 Apr-12-2024, 05:13 AM
Last Post: Pedroski55
  Help creating shell scrip for python file marciokoko 10 1,380 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Need help with 'str' object is not callable error. Fare 4 860 Jul-23-2023, 02:25 PM
Last Post: Fare
  Creating csv files from Excel file azizrasul 40 5,716 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  Creating list of lists, with objects from lists sgrinderud 7 1,660 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Error in Int object is not subscript-able. kakut 2 1,190 Jul-06-2022, 08:31 AM
Last Post: ibreeden
  Creating file with images BobSmoss 1 1,396 Jan-08-2022, 08:46 PM
Last Post: snippsat
  Error creating database with python and form? shams 3 2,384 Aug-02-2021, 02:00 PM
Last Post: deanhystad
  Is there a library for recursive object creation using config objects johsmi96 0 1,857 May-03-2021, 08:09 PM
Last Post: johsmi96
Star Type Error: 'in' object is not callable nman52 3 3,418 May-01-2021, 11:03 PM
Last Post: nman52

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020