python3 wx.TreeCtrl - how to iterate through several levels


Keywords:python  3.x 


Question: 

I have a treectrl structure which is populated from an external search of an open data set hosted by our municipal government. The data pertains to business licenses and is requested using Pandas and Sodapy. The tree is populated as follows:

for index, row in results_df.iterrows():
        tradename = row['tradename']
        address = row['address']
        licTypes = row['licencetypes']
        comm = row['comdistnm']
        jobSts = row['jobstatusdesc']
        jobCrt = row['jobcreated']
        lng = row['longitude']
        lng = str(lng)
        lat = row['latitude']
        lat = str(lat)

        # Populate Tree Controls with DataFrame values

        trdName = self.thrTree.AppendItem(root, tradename)
        self.thrTree.AppendItem(trdName, address)
        self.thrTree.AppendItem(trdName, licTypes)
        self.thrTree.AppendItem(trdName, comm)
        self.thrTree.AppendItem(trdName, jobSts)
        self.thrTree.AppendItem(trdName, jobCrt)
        self.thrTree.AppendItem(trdName, lng)
        self.thrTree.AppendItem(trdName, lat)

This will result in a final structure of root, then node 1 with business name, and when expanded, contains all the information listed above, so I'm assuming root level, then child node 1, then child.child of node 1? Not even sure how the second second indented nodes are called. (I've heard the term leaf for the third level used before) But I digress; what I am interested in is grabbing the Latitude and Longitude of where the business is located, then allowing the user to map the location if they choose. I bind a wx.EVT_TREE_ITEM_ACTIVATED so that when the user double clicks on a business name to get the details, I want to grab the items displayed. This is how I am currently trying to iterate through the child nodes.

item = self.thrTree.GetSelection()

    while self.thrTree.GetItemParent(item):
        piece = self.thrTree.GetItemText(item)
        tmpHldr.insert(0, piece)
        item = self.thrTree.GetItemParent(item)

Looking at item, it appears to be collecting all the business names under root, and ignoring the third level items of interest.

What do I need to do to go deeper within the tree to grab the details under the business clicked on, and not just the list of business names under the root item, which is called 'Search Results'?

Thanks!


1 Answer: 

@YYC_Code,

Did you look here? This has GetFirstChild()/GetNextChild() pair functions that you can use to iterate. It also has ItemHasChildren() function which you can use to verify if the item has any children and use the pair mentioned above if it does.

EDIT:

[quote]

For this enumeration function you must pass in a ‘cookie’ parameter which is opaque for the application but is necessary for the library to make these functions reentrant (i.e. allow more than one enumeration on one and the same object simultaneously). The cookie passed to GetFirstChild and GetNextChild should be the same variable.

[/quote]

You need to make sure that the cookie parameter is the same during the iteration.

You should also do this:

[quote]

Returns an invalid tree item (i.e. wx.TreeItemId.IsOk returns False) if there are no further children.

[/quote]