为什么pickle看不到“painter”类?加载时?
我正在使用 oop 并尝试使用 pickle 加载我保存在 .txt 文件中的行列表。我可以用pickle保存数据,但我不知道为什么在初始化后它看不到“画家”。
class LoadButton(MTButton):
def __init__(self, **kwargs):
super(LoadButton, self).__init__(**kwargs)
self.buttonLoad = kwargs.get('painter')
def on_release(self, touch):
if touch.device != 'wm_pen':
newLoad = self.buttonLoad
loadFile = open('savefiles/savetest.txt', 'rb')
newpainter = painter
scatter.remove_widget(painter) # if removed error: EOF, no data read
# error: local var 'painter' referenced before assignment
oldlines = pickle.load(loadFile)
painter = newpainter
scatter.add_widget(painter)
pprint.pprint(oldlines)
loadFile.close()
return True
任何帮助都会很棒。谢谢。
I am working with a o.o.p and trying to use pickle to load a list of lines I save in a .txt file. I can save the data with pickle, but I am not sure why it can't see 'painter' after I have initialized it.
class LoadButton(MTButton):
def __init__(self, **kwargs):
super(LoadButton, self).__init__(**kwargs)
self.buttonLoad = kwargs.get('painter')
def on_release(self, touch):
if touch.device != 'wm_pen':
newLoad = self.buttonLoad
loadFile = open('savefiles/savetest.txt', 'rb')
newpainter = painter
scatter.remove_widget(painter) # if removed error: EOF, no data read
# error: local var 'painter' referenced before assignment
oldlines = pickle.load(loadFile)
painter = newpainter
scatter.add_widget(painter)
pprint.pprint(oldlines)
loadFile.close()
return True
Any help would be awesome. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
painter = newpainter
创建了一个局部变量painter
,即使在调用全局painter
时的部分之后也是如此。做这样的事情:
编辑:但是你为什么不只使用
painter
呢?编辑2:
例子:
It is because
painter = newpainter
creates a local variablepainter
, even if after the part when you call the globalpainter
.Do something like this:
EDIT: But why don't you use only
painter
?EDIT 2:
Example: