2

如何通过树的索引获取树项的数据?

我目前正在用 wxPython 编写一个 GUI(使用 Python 2.7)。此 GUI 包含一个 CustomTreeCtrl,其构建如下:

    # Create Tree control
    widget_tree = CustomTreeCtrl(parent=self, id=ID_WIDGET_TREE, agwStyle=wx.TR_HIDE_ROOT | wx.TR_SINGLE | wx.TR_HAS_BUTTONS, 
                                 size=(UI_tree_width, -1))
    widget_tree.SetBackgroundColour(wx.WHITE)
    root = widget_tree.AddRoot(text="root")

# Create general
    item_general = widget_tree.AppendItem(parentId=root, text='General')
    item_main = widget_tree.AppendItem(parentId=item_general, text='Main', data={'tooltip': 'Main'})
    widget_tree.AppendItem(parentId=item_general, text='Manual', data={'tooltip': 'Manual'})
    widget_tree.AppendItem(parentId=item_general, text='Boundary conditions', data={'tooltip': 'BC'})

    # Create stiffeners
    item_stiffener = widget_tree.AppendItem(parentId=root, text='Stiffeners')
    widget_tree.AppendItem(parentId=item_stiffener, text='Stiffener 1', data={'tooltip': 'Stiffener 1'})
    widget_tree.AppendItem(parentId=item_stiffener, text='Stiffener 2', data={'tooltip': 'Stiffener 2'})
    widget_tree.AppendItem(parentId=item_stiffener, text='Add stiffener', data={'tooltip': 'Creates a new stiffener'})

数据将通过用户的某些输入进行扩展。除此之外,单击“添加加强筋”将在“添加加强筋”上方插入一个项目。出于该工具的目的,我需要从每个加强筋中获取这些数据。我想知道是否可以在列表中调用这些类似列表。因此,例如,我可以为 [root][stiffeners][stiffener 1] 调用 widget_tree.GetTreeItem[0][1][0] 等等。

我一直在寻找一段时间,希望有人可以帮助我。提前致谢!

4

1 回答 1

1

我继承了一个项目,该项目有一个非常复杂的方法来做你要求的事情,但你的帖子给了我一个新的想法。从概念上讲,我认为您在谈论类似于get_item_by_indexes此示例中的方法的内容(我保留了所有调试打印):

import wx
from wx.lib.agw.customtreectrl import CustomTreeCtrl

class IndexTree(CustomTreeCtrl):
def get_item_by_indexes(self, *args):
    args = list(args)  # copy so we don't modify original
    print("Trying tree index: %s" % str(args))
    if args[0] > 0:
        raise IndexError("Invalid root index")
    parent = self.GetRootItem()
    print("arg_index=0, tree_index=0, item=%s" % (parent.GetText()))
    for arg_index, tree_index in enumerate(args[1:], 1):
        parent = parent.GetChildren()[tree_index]
        print("arg_index=%d, tree_index=%d, item=%s" % (arg_index, tree_index, parent.GetText()))
    return parent

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.DefaultSize)
        widget_tree = IndexTree(parent=self, agwStyle=wx.TR_HIDE_ROOT | wx.TR_SINGLE | wx.TR_HAS_BUTTONS, 
                                 size=(400, -1))
        widget_tree.SetBackgroundColour(wx.WHITE)
        root = widget_tree.AddRoot(text="root")

    # Create general
        item_general = widget_tree.AppendItem(parentId=root, text='General')
        item_main = widget_tree.AppendItem(parentId=item_general, text='Main', data={'tooltip': 'Main'})
        widget_tree.AppendItem(parentId=item_general, text='Manual', data={'tooltip': 'Manual'})
        widget_tree.AppendItem(parentId=item_general, text='Boundary conditions', data={'tooltip': 'BC'})

        # Create stiffeners
        item_stiffener = widget_tree.AppendItem(parentId=root, text='Stiffeners')
        widget_tree.AppendItem(parentId=item_stiffener, text='Stiffener 1', data={'tooltip': 'Stiffener 1'})
        widget_tree.AppendItem(parentId=item_stiffener, text='Stiffener 2', data={'tooltip': 'Stiffener 2'})
        widget_tree.AppendItem(parentId=item_stiffener, text='Add stiffener', data={'tooltip': 'Creates a new stiffener'})

        widget_tree.get_item_by_indexes(0)
        widget_tree.get_item_by_indexes(0, 1)
        widget_tree.get_item_by_indexes(0, 0, 0)
        widget_tree.get_item_by_indexes(0, 1, 0)
        widget_tree.get_item_by_indexes(0, 1, 2)
        try:
            widget_tree.get_item_by_indexes(0, 1, 8)
        except IndexError, e:
            print(e)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'IndexTree')
        frame.Show(True)
        frame.Centre()
        return True

if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()

对于测试用例产生:

Trying tree index: [0]
arg_index=0, tree_index=0, item=root
Trying tree index: [0, 1]
arg_index=0, tree_index=0, item=root
arg_index=1, tree_index=1, item=Stiffeners
Trying tree index: [0, 0, 0]
arg_index=0, tree_index=0, item=root
arg_index=1, tree_index=0, item=General
arg_index=2, tree_index=0, item=Main
Trying tree index: [0, 1, 0]
arg_index=0, tree_index=0, item=root
arg_index=1, tree_index=1, item=Stiffeners
arg_index=2, tree_index=0, item=Stiffener 1
Trying tree index: [0, 1, 2]
arg_index=0, tree_index=0, item=root
arg_index=1, tree_index=1, item=Stiffeners
arg_index=2, tree_index=2, item=Add stiffener
Trying tree index: [0, 1, 8]
arg_index=0, tree_index=0, item=root
arg_index=1, tree_index=1, item=Stiffeners
list index out of range

一个很大的警告,我还没有在实践中使用它,因为在刚刚看到你的帖子之前我没有这个想法。但是我一起破解了这个,我很想看看我是否可以替换我的项目过于复杂的方法。

于 2017-05-05T05:52:15.423 回答