I'm making a tree ctrl and need to figure out where the double click came from.

Code:

def _initElementsOfTreePanel(self, panel):
    tree = wx.TreeCtrl(panel, -1, wx.Point(0, 0), wx.DefaultSize, wx.NO_BORDER | wx.TR_DEFAULT_STYLE)

    root = tree.AddRoot("Elements")
    items = []
    imglist = wx.ImageList(16, 16, True, 2)
    imglist.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))
    imglist.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, wx.Size(16, 16)))
    tree.AssignImageList(imglist)

# #        Create the element Tree (the left hand side panel). Places in it all the found functions 
    def appendSubitems(item, func_list, tree):
        print item
        for func in func_list:
            itemid = tree.AppendItem(item, func.__name__, 1)
            print"GUIFrame._initElementsofTreePanel, func:",func.__name__,"id:",itemid

#        print "TreeItemId:",
    appendSubitems(tree.AppendItem(root, "Input functions", 0), self._elemdisco.input_functions, tree) 
    appendSubitems(tree.AppendItem(root, "Processing functions", 0), self._elemdisco.processing_functions, tree)
    appendSubitems(tree.AppendItem(root, "Output functions", 0), self._elemdisco.output_functions, tree)
    appendSubitems(tree.AppendItem(root, "Other functions", 0), self._elemdisco.other_functions, tree)


    tree.ExpandAll()
    self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self._addNodeToSchema, self.treePanel.tree)

def _addNodeToSchema(self,event):
    print event.GetItem()

Now the output shows that no matter what node double click on, it recognizes the same item to have fired the event. And it corresponds to no ID of any item that i know i've added, and no ID of the root.

Anyone know how I can get a reference to the object in the tree? I basically want to get its name, but this API won't let me :P

[EDIT]:Solver, but i think this is a framework bug. There's no reason why the event should return the same TreeItemID regardless of where the double click actually occurs.

有帮助吗?

解决方案

def _addNodeToSchema(self,event):
    pt = event.GetPoint()
    item, _ = self.treePanel.tree.HitTest(pt)
    if item:        
        print self.treePanel.tree.GetItemText(item) #this is the label of the node

Apparently one must get the point position, and inquire the tree about any elements it has that can be found at that position :) Not very OO, but i'll take it.

其他提示

'''multiple selection with drag event, shows the dragged items

def On_Drag(self,event):
    select = (self.custom_tree.GetSelections())
    for i in range(len(select)):
        print(self.custom_tree.GetItemText(select[i]))'''
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top