Tkinter, pickle (or shelve) and marshal

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Nov 5 10:22:42 CET 2001


Grzegorz Dostatni <grzegorz at ugrad.cs.ualberta.ca> writes:

> I am looking  for a beautifull, Pythonic way to solve this ;-)

The Pythonic way for pickling objects is to implement a
__getstate__/__setstate__ pair. You can have __getstate__ return
whatever you want, and have __setstate__ do an "window-reopening"
actions you may need.

In doing so, you'll find that Tkinter widgets currently don't
implement __getstate__/__setstate__. I see two alternatives:

1. Derive from each Widget class you use, and add those methods into
   the derived class. Then use your derived widget classes instead of
   the original ones. This is the clean OO way of life.

2. Come up with a generic implementation of getstate/setstate, and
   "inject" them into some Tkinter base class. There are again
   different ways to do that; one is to put the methods into a
   separate class, and assign to Tkinter.Misc.__bases__. This is the
   hacky, introspectional way of life.

HTH,
Martin




More information about the Python-list mailing list