我的問題是,當我在我的樹中有一個節點時,它不會將樹保存到文件。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