我在主应用程序关闭后试图挑选一个类实例但是我得到了
Exception AttributeError: "'NoneType' object has no attribute 'dump'" in <bound method MainFrame.__del__ of <__main__.MainFrame object at 0x03BA6508>> ignored
以下是示例代码:
from PySide.QtGui import *
import sys
import pickle
class Progress:
def __init__(self, value):
self.x = value
def __del__(self):
pickle.dump(self, open("pickle_file.p", "wb"))
class MainFrame(QWidget):
def __init__(self):
QWidget.__init__(self)
pass
# def __del__(self):
# pickle.dump(progress, open("pickle_file.p", "wb"))
if __name__ == "__main__":
try:
with open("pickle_file.p", "r") as p_file:
progress = pickle.load(p_file)
except (EOFError, IOError):
progress = Progress(1)
app = QApplication(sys.argv)
main = MainFrame()
main.show()
sys.exit(app.exec_())
__del__
的两种方法都会引发相同的错误。
我该怎么做?
答案 0 :(得分:1)
永远不要使用__del__
,除非你真的,真的知道你在做什么。
如果您想在退出时保存,请重新实现主窗口的closeEvent
:
class MainFrame(QWidget):
...
def closeEvent(self, event):
pickle.dump(progress, open("pickle_file.p", "wb"))