泡菜和tkinter的问题 [英] problem with pickle and tkinter

查看:102
本文介绍了泡菜和tkinter的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要学习tkinter,我正在制作一个简单的Go游戏程序.现在,我希望能够使用泡菜保存游戏,但是当我尝试对我的GoBoardModel对象进行泡菜时,我得到了:

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>

我想这是由于以下事实:GUI和go board的模型被很好地分开了,但是该模型仍然具有对视图的引用,以便推送某些内容,因此pickle可能会尝试对tk进行pickle东西.当然,我只想腌制模型,所以有什么办法告诉腌制者不要在意对GUI的引用吗?还是解决该问题的另一种方法?

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

推荐答案

是的,请查看__getstate__方法.

例如,如果您希望泡菜忽略'view'属性,则可以执行以下操作:

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

这篇关于泡菜和tkinter的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆