为什么pickle看不到“painter”类?加载时?

发布于 2024-11-14 04:52:37 字数 854 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-11-21 04:52:37

这是因为 painter = newpainter 创建了一个局部变量 painter,即使在调用全局 painter 时的部分之后也是如此。
做这样的事情:

painter_ = newpainter
scatter.add_widget(painter_)

编辑:但是你为什么不只使用 painter 呢?

        scatter.remove_widget(painter)
        oldlines = pickle.load(loadFile)
        scatter.add_widget(painter)

编辑2:
例子:

>>> bar = 'Bar'
>>> def foo():
...     bar  # This is the local bar. It has not been assigned a value yet.
...     bar = 'Local Bar'  # Here I assign a value to the bar.
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'bar' referenced before assignment
>>> 

It is because painter = newpainter creates a local variable painter, even if after the part when you call the global painter.
Do something like this:

painter_ = newpainter
scatter.add_widget(painter_)

EDIT: But why don't you use only painter?

        scatter.remove_widget(painter)
        oldlines = pickle.load(loadFile)
        scatter.add_widget(painter)

EDIT 2:
Example:

>>> bar = 'Bar'
>>> def foo():
...     bar  # This is the local bar. It has not been assigned a value yet.
...     bar = 'Local Bar'  # Here I assign a value to the bar.
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'bar' referenced before assignment
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文