problem with pickle and tkinter

不打扰是莪最后的温柔 提交于 2019-12-29 09:15:34

问题


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 :)


回答1:


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


来源:https://stackoverflow.com/questions/5972445/problem-with-pickle-and-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!