我的问题是,当我在我的树中有一个节点时,它不会将树保存到文件。Python wxTree将树保存到文件
的代码保存到文件是:
def project_save(self):
try:
output = open(self.project_file, 'w+')
output.write(self.tree.GetItemText(self.root) + '\n')
count = self.tree.GetChildrenCount(self.root)
iter = 0
child = ''
for i in range(count):
if i == 0:
child, cookie = self.tree.GetFirstChild(self.root)
else:
child, cookie = self.tree.GetNextChild(self.root, cookie)
output.write(self.tree.GetItemText(child) + '\n')
output.close()
self.projectdirty = False
except IOError:
MsgDlg(self, 'There was an error saving the new project file.', 'Error!', wx.OK)
当我的树看起来像:
root
|
---item
|
---item
它工作得很好
但是当我的树是这个样子:
root
|
---item
|
---item
|
-node
|
---item
|
---item
它给我一个空白
File "project_manager.py"
output.write(self.tree.GetItemText(child) + '\n')
File "C:\Python27\lib\sit-packages\wx-2-8-msw-ansi\wx\contrls.py", line 5303, in GetItemText
return controls.TreeCtrl_GetItemText(*args, **kwargs)
wx._core.PyAssertionError: c++ assertion "item.IsOk()" failed at ....\src\msw\treectrl.cpp(963) in wxTreeCtrl::GetItemText(): invalid tree item
这不正是我之后,我的程序允许另一个文件的打开和它改变由该文件中定义的树。如果有节点,我可以将它保存到文件中,这真的很有帮助。谢谢回复 :) – SC7639