Pickle funktioniert nicht

Fragen zu Tkinter.
Antworten
leonards
User
Beiträge: 18
Registriert: Samstag 9. Juni 2012, 10:59

Hallo,
ich habe ein Teil eines Programms, das nicht funktioniert.
Der fehlerhafte Code ist:

Code: Alles auswählen

self.stufe = str(self.stufe)
neu = {self.wort:[self.formen,self.geschlecht,self.deutsch,self.stufe]}
vokabeln = self.vokabeln
vokabeln.update(neu)
f = file('vokabeln.txt','w')
pickle.dump(vokabeln,f)
f.close()
Die Variablen exestieren alle, und sind auch im richtigen Typ.
Der fehler muss in diesem Programmteil sein, da der Fehler bei aufführen der Definition auftritt, die nur diesen Text beinhaltet. Die Fehlermeldung lautet:

Code: Alles auswählen

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "D:\Programmieren\Python\Daten\Programme\no-console\vokabeln\einzeln.pyw", line 49, in event
    self.veraendern()
  File "D:\Programmieren\Python\Daten\Programme\no-console\vokabeln\einzeln.pyw", line 60, in veraendern
    pickle.dump(vokabeln,f)
  File "C:\Python27\lib\pickle.py", line 1370, in dump
    Pickler(file, protocol).dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 600, in save_list
    self._batch_appends(iter(obj))
  File "C:\Python27\lib\pickle.py", line 615, in _batch_appends
    save(x)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 313, in save
    (t.__name__, obj))
PicklingError: Can't pickle 'tkapp' object: <tkapp object at 0x026EDD78>
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "D:\Programmieren\Python\Daten\Programme\no-console\vokabeln\einzeln.pyw", line 49, in event
    self.veraendern()
  File "D:\Programmieren\Python\Daten\Programme\no-console\vokabeln\einzeln.pyw", line 60, in veraendern
    pickle.dump(vokabeln,f)
  File "C:\Python27\lib\pickle.py", line 1370, in dump
    Pickler(file, protocol).dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 600, in save_list
    self._batch_appends(iter(obj))
  File "C:\Python27\lib\pickle.py", line 615, in _batch_appends
    save(x)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 663, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 313, in save
    (t.__name__, obj))
PicklingError: Can't pickle 'tkapp' object: <tkapp object at 0x026EDD78>
Vielen Dank für Hilfe.
Gruß Leonard :)
BlackJack

@leonards: Die Meldung ist ziemlich deutlich: Du versuchst etwas mit `pickle` zu serialisieren was man nicht serialisieren kann. In diesem Fall ein Objekt was mit `Tk` zu tun hat. Versuche nur das nötigste zu serialisieren und nicht Objekte die Referenzen auf andere Objekte haben, die gar nicht wirklich relevant für die Daten sind. Was ist den `vokabeln` für ein Objekt?

Weitere Anmerkung: `pickle`-Daten sind Binärdaten und deshalb muss man Dateien im Binärmodus öffnen. Sonst kann es passieren, dass die Daten nicht wieder gelesen werden können. Ausserdem sollte man Dateien mit der ``with``-Anweisung verwenden, damit man sicher sein kann, dass sie unter allen Umständen auch geschlossen werden.
leonards
User
Beiträge: 18
Registriert: Samstag 9. Juni 2012, 10:59

'vokabeln' ist ein dictionary, was ich vorher geöffnet habe
Gruß Leonard :)
EyDu
User
Beiträge: 4881
Registriert: Donnerstag 20. Juli 2006, 23:06
Wohnort: Berlin

leonards hat geschrieben:'vokabeln' ist ein dictionary, was ich vorher geöffnet habe
Dann zeig doch mal WIE du das Dictionary gefüllt hast. Wie es aussieht, scheinen darin ja einige Tkinter-Objekte gelandet zu sein.
Das Leben ist wie ein Tennisball.
Antworten