【发布时间】:2017-06-20 15:05:12
【问题描述】:
我正在使用 wxpython 开发一个 GUI 应用程序,它有大约 110 个用户选择的参数。因为我希望用户能够将这些选项保存到项目文件中,所以我决定使用 wxPython 中包含的 PersistenceManager 模块。
只要我不尝试指定保存设置的文件名,即我使用默认值 (C:\users\username\AppData\programName\Persistence_Options),并且让程序在退出时保存设置,持久性就可以很好地工作.
我想要做的是允许用户选择一个文件来保存设置(因为他们可能有多个具有不同选项的项目)。但是,当我将SetPersistenceFile 方法与用户指定的文件名一起使用时,没有文件被保存,也没有返回错误消息,即使它确实在执行下面给出的那些代码行。 (OnSave函数是程序主窗口的方法。)
def OnSave(self, e):
self.dirname = os.getcwd()
if self.ProjectFile == '':
dlg = wx.FileDialog(self, "Save project file", self.dirname, "", "Project configuration file (.prj)|*.prj", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_CANCEL:
return
else:
self.ProjectFile = os.path.join(dlg.GetDirectory(), dlg.GetFilename())
#print self.ProjectFile
if self.ProjectFile != '':
print "Made it to here (Save)..."
#self.Register(self) # Also tried calling Register in __init__
self._persistMgr = PM.PersistenceManager.Get()
print self.ProjectFile # Gives correct filename
self._persistMgr.SetPersistenceFile(self.ProjectFile)
self._persistMgr.Save(self)
print "Finished saving."
我尝试使用本地 PersistenceManager 对象,而不是将其作为类成员,这没有任何区别。有趣的是,如果我在窗口的__init__ 函数中声明self.__persistMgr 对象并在那里使用带有硬编码文件名的SetPersistenceFile 方法,它会写入文件,但是这没有帮助,因为用户需要指定在运行时。
有谁知道为什么文件没有保存以及如何解决这个问题?
【问题讨论】:
标签: python-2.7 wxpython