我继承了一个项目,该项目有一个非常复杂的方法来做你要求的事情,但你的帖子给了我一个新的想法。从概念上讲,我认为您在谈论类似于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
一个很大的警告,我还没有在实践中使用它,因为在刚刚看到你的帖子之前我没有这个想法。但是我一起破解了这个,我很想看看我是否可以替换我的项目过于复杂的方法。