Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

python - problem with pickle and tkinter

To learn tkinter I'm making a simple Go game program. I now would like to be able to save a game using pickle, but when I try to pickle my GoBoardModel object I get:

PicklingError: Can't pickle 'tkapp' object: <tkapp object at 0x01FCB090>

I guess that comes from the fact that while the GUI and the model of the go board are quite well separated, the model still has a reference to the view in order to push some stuff, so pickle probably tries to pickle some tk stuff. Of course I would like to pickle just the model, so is there any way to tell pickle not to care about that reference to the GUI? Or another way to go around the problem?

I know I could just get rid of this reference, but I'm here to learn :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yep, look into the __getstate__ method.

For example, if you want pickle to ignore the 'view' attribute, you'd do the following:

class Whatever(object):

  def __getstate__(self):
    state = self.__dict__.copy()
    del state['view']
    return state

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...