Putting Leaves on wx.TreeCtrl() Twigs

Presumably you've got some way in your data storage to associate your leaf
data items with their parent, if not then that is where you need to start.

Robin,

   I believe that I do have the leaves properly associated with their twigs.
The item ID hierarchy is explicit for the root and the branches, but not for
the twigs:

   rootID
     >
     +- natID
     >
     +- ecoID
     >
     +- socID

To add them to the tree you can simply locate all the leaf items for a
particular twig when you add that twig, and then add the leaves to the
twig the same way.

   The twigs are appended to the proper branches with, for example,

   for item in self.appData.polNat:
           ngc = self.polTree.AppendItem(self.natID, item[0])

   The leaves are now correctly associated with their twigs (verified by
adding 'print' statements to print twigs and leaves), thanks to Tom's help:

   policy = self.polTree.GetItemText(ngc)
         subpolicies = [sp[0] for sp in self.appData.subPols if sp[1]==policy]

   But, the variable, 'policy,' is not the itemID for the twig, but the
string identifying each twig. So, when I try

         spol = self.polTree.AppendItem(???, subpolicies)

I don't know what to put for the itemID field where the question marks are.

In other words, your data storage should have some way of representing the
hierarchy of the items, and your treectrl simply needs to reflect that. Start first with the data model, and then worry about the view. If the
model is done right then the view should be fairly simple.

   The twigs and leaves (policies and subpolicies) are held in two tables in
the SQLite database.

Rich

···

On Fri, 15 Dec 2006, Robin Dunn wrote:

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Rich Shepard <rshepard <at> appl-ecosys.com> writes:

   The variable, 'ngc,' is what's being returned, and that seems to work OK
as the itemID, so I guess that my problem is what to put as the second
(i.e., text) parameter in the AppendItem() call.

   If I follow the above with

   policy = self.polTree.GetItemText(ngc)
   subpolicies = [sp[0] for sp in self.appData.subPols if sp[1]==policy]
         spol = self.polTree.AppendItem(ngc, sp[0])

there are no errors generated. However, what is appended to each policy
(twig) is the last subpolicy (leaf) in that database table. It is appended
to all twigs. While printing policy and subpolicy produces the correct
results (that is, a blank list when there are no subpolicies and the correct
subpolicies for each policy), I'm not appending them correctly. The last
line appends only the last subpolicy (sp[0]) in the database to all
policies.

Hi Rich,

The problem with the above is that you are not using the subpolicies list that
you created. I think you want to do something like this:

...
subpolicies = [sp[0] for sp in self.appData.subPols if sp[1]==policy]
for subpol in subpolicies:
    self.polTree.AppendItem( ngc, subpol )

Note that I haven't assigned the return value of AppendItem() to a variable.
I am assuming from your previous messages that this is the deepest level of
the tree and you will not be adding any children to these items. If that is
not the case, or if you need to know/store the TreeItemID returned, then you
can of course add an assignment to the above.

Brian

Werner,

   Despite appearances, I really do understand this. :slight_smile:

   It is when the next level is appended that I do not have explicit
assignments to an itemID for each one.

   Thinking about the code last evening and this morning I can better
identify what I don't know how to do. I now have the correct variable for
the next level -- the twigs in my analogy -- (it's 'ngc'), and the correct
item text for the terminal leaves for each twig (it's 'subpolicies'). But,
I'm not correctly stepping through the lists. Instead of appending the
subpolicies (if there are any) to the appropriate policy, the final
subpolicy in the list is appended to all policies. That's not what's needed.

   So, the last line in the code is incorrect and I need to fix that:
   spol = self.polTree.AppendItem(ngc, sp[0])

   The previous lines produce the desired results when I print the LHS

   for item in self.appData.polNat:
     ngc = self.polTree.AppendItem(self.natID, item[0])
     policy = self.polTree.GetItemText(ngc)
     print policy, '\n'
     subpolicies = [sp[0] for sp in self.appData.subPols if sp[1]==policy]
     print subpolicies, '\n'

produces, in part

air quality

water quality

[u'organics', u'metals', u'turbidity', u'DO', u'pH']

slope stability

[u'soil depth', u'grade', u'vegetation density', u'aspect']

   Where I'm failing to write the proper code is appending the list contents
to the tree item immediately above them. In this example, 'air quality' is
properly appended to the tree, and has no assigned subpolicies. 'water
quality' is properly appended to the tree, but its subpolicies are not
assigned because I don't explicitly know the itemID of 'water quality'.

   I believe it's my lack of experience with python more than wxPython. I
know how I'd do this in C, but that's not helping me now.

Thanks,

Rich

···

On Tue, 19 Dec 2006, Werner F. Bruhin wrote:

Maybe the following helps.

for natItem in whateverdatasource:
____natID = treectrl.AppendItem(rootID, 'yourtext')
____for ecoItem in whateverdatasource:
________ecoID = treectrl.AppendItem(natID, 'yourtext')
________for socItem in whateverdatasource:
____________socID = treectrl.AppendItem(ecoID, 'yourtext')

At each level you pass the ID of the previous level.

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863