【问题标题】:Python wxTree saving the tree to filePython wxTree 将树保存到文件
【发布时间】:2012-05-04 13:42:08
【问题描述】:

我的问题是,当我的树中有一个节点时,它不会将树保存到文件中。

保存到文件的代码是:

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

它给了我一个错误,我的文件最终是空白的

文件“project_manager.py”

output.write(self.tree.GetItemText(child) + '\n')

文件 "C:\Python27\lib\sit-packages\wx-2-8-msw-ansi\wx\contrls.py", 第 5303 行,在 GetItemText 中

返回控件.TreeCtrl_GetItemText(*args, **kwargs)

wx._core.PyAssertionError: c++ 断言“item.IsOk()”失败于 ....\src\msw\treectrl.cpp(963) 在 wxTreeCtrl::GetItemText() 中:无效 树项

【问题讨论】:

    标签: python tree wxpython


    【解决方案1】:

    几年前,PersistentControls 模块发布,它可以帮助您保存和恢复 wxPython 中的几乎所有小部件。请参阅以下链接:

    【讨论】:

    • 这不是我所追求的,因为我的程序允许打开另一个文件,并且它会更改该文件定义的树。如果有节点,如果我能把它保存到文件中,那真的很有帮助。感谢您的回复:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2019-10-30
    • 2016-08-25
    相关资源
    最近更新 更多