how to pickle treectrl?

That's not going to work very well. The only cases that will pickle at all
well are those that are implemented 100% in Python, and those are rare
indeed (most classes implemented in Python actually use native wx super
classes anyway).

The native wx objects are (correct me if I'm wrong Robin) wrappers around
pointers to C library classes that are in memory, so in essense if you even
pickle something as simple as wx.Colour, it's unusable when you unpickle it
because its memory pointers are pointing at total trash.

I'm sure I'm off on one or two points but that's the gist of it -- Python
knows how to pickle its own objects, but not objects from a totally alien
landscape.

···

-----Original Message-----
From: Bert Marco Schuldes [mailto:bert@schuldes.org]
Sent: Monday, September 29, 2003 05:37
To: wxPython-users@lists.wxwindows.org
Subject: [wxPython-users] how to pickle treectrl?

Hallo,

I am pretty new to python and wxwindows.

I tried in vain to pickle a wxtreectl with
children and subchildren and data associated
to each node.

a simple dump(tree, file) does not do the trick.

is there any simple solution for this problem?

Thank you,

Marco

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Jeff Grimmett wrote:

The native wx objects are (correct me if I'm wrong Robin) wrappers around
pointers to C library classes that are in memory, so in essense if you even
pickle something as simple as wx.Colour, it's unusable when you unpickle it
because its memory pointers are pointing at total trash.

Almost correct. The wxPython instances have a string attribute that is used as a pointer to the C++ object so when it gets pickled that is just saved as a string and not dereferenced to get the C++ data. But some classes like wxColour, wxSize, etc. have had __getstate__, __setstate__ and __getinitargs__ added so they can be pickled. In theory you could do the same with any of the classes, but in practice I expect that it would be very difficult to find a way to get pickle to correctly construct a window class. Probably better is to separate your data from your view classes and then add methods to the vew class that can dump/load the data using pickle.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Almost correct.

... and they said I sucked at horseshoes, too. :slight_smile:

classes like wxColour, wxSize, etc. have had __getstate__, __setstate__

               ^^^^^^^^

and __getinitargs__ added so they can be pickled. In theory you could

Uh oh. Last time I tried pickling a wxColour instance, it blew chunks all
over my screen when I unpickled it.

To be fair, I've noticed that anything but mode 0 in the pickle module seems
to lose its integrity at some point, and I was probably using mode 1 at that
point in time, blissfully unaware of that pitfall. I'll look back into it
and let you know if wxColour still exhibits probs with pickling.

construct a window class. Probably better is to separate your data from
your view classes and then add methods to the vew class that can
dump/load the data using pickle.

Yeah, that's usually the way I go too.