Help with treeview population

191 views
Skip to first unread message

Matheus Baumgarten

unread,
Feb 12, 2020, 10:06:36 AM2/12/20
to Kivy users support
Hey group, first time here.
    I've picked up kivy as my first GUI framework to work on a project that involves data manipulation.
    My app already inserts data into tables and retrieve that data to edit it, but now I'm stuck trying to display that data in a treeview.
    I can't get to understand how kivy treeview works.
    I'm using sqlite3 for my data.
    I'm getting for example: [('code', 'name', 'parent')] from my db:
    
cur.execute('SELECT code, name FROM sometable WHERE parent IS NULL')
data = cur.fetchall()
tv = TreeView()
for n in range(len(data)):
   tv.add_node(TreeViewLabel(text=f'{data[n][0]} {data[n][1]}'))

    So I need to add x parent nodes to the Treeview if the db list has no parent like:
            Root TV:
                'code' 100  'name' something
                'code' 200 'name' anotherthing
                <more codes...>
    And then after that I need to insert into 'code' 100 node some more data, like:
       Root Tv:
          'code' 100 'name' something
               'code' 100.10 'name' more something 
          'code' 200 'name' anotherthing
               <need to latter insert here too>
          <more codes...>

    And so on as the user of my app adds more data into the db (it can get over 100.10.1.1.1.1.1 ... if the user has lots of data...)
    But I can't get to undertand how to add data to an existing node ('code' 100).
    I've searched online and none of the codes I found helped me, and I didn't find anithing relating to updating an already created node.

    I'd really appreciate if you guys could get me any tips on how to do it, or do something near the example.
    Obs:  I'm very new to coding. I've been practicing kivy a lot and by now I understand most of it's stuff.

Cheers,
Matheus Baumgarten.

Robert Flatt

unread,
Feb 12, 2020, 12:34:36 PM2/12/20
to Kivy users support
The requirement is a hierarchical tree?


The second example:

tv = TreeView()
n1 = tv.add_node(TreeViewLabel(text='Item 1'))
tv.add_node(TreeViewLabel(text='SubItem 1'), n1)
tv.add_node(TreeViewLabel(text='SubItem 2'), n1)

Note the extra parameter for nodes not at the root (those with text 'SubItem X' in the example).
The parameter specifies the parent node.

Matheus Baumgarten

unread,
Feb 12, 2020, 5:08:23 PM2/12/20
to Kivy users support
Yeah, I'm aiming for a hierarchical tree, but I'm not sure how to sort my data for the node insertion, the docs only show the dict() way and I'm receiving tuples inside a list [ ( code, name) (code, name, parent ) ( ) ]
    I need to filter the data that don't have any parents and show them at the same nesting level, and then add more nodes to them but I'm not seeing any way of inserting data into an already existing node.
    Thanks.

Robert Flatt

unread,
Feb 12, 2020, 7:11:08 PM2/12/20
to Kivy users support
So you have a flat data structure containing hierarchy information, and you want to map that to a hierarchical structure?
In addition in order to populate TreeView you need to traverse each level of this hierarchy in an order starting at the root?

The following assumes 'name' is unique over the tree.

1) Build the hierarchical structure:
Build a dict keyed by name, and the value is the db row as a list. Add extra an field  'children' (an empty list)
For each entry in the dict, add the entry name to the 'children' list of its parent dict entry (you know the parent's name).

2) Build TreeView
Walk the tree starting at the root, creating the TreeViewLabel node, and stepping though the children.
Pass the parent node to the child, and create the child, recur.

Assuming I didn't mess up, since I'm typing about it and not actually doing it. That should be pretty close to what you need.

This is more about Python programming than Kivy. The important thing for Kivy TreeView is you need to be able to walk the data structure from root to leaf, which means having a hierarchical data structure. So create that data structure first, since what you have is flat.

Any other Kivy questions, please start a new thread.

Elliot Garbus

unread,
Feb 12, 2020, 7:35:59 PM2/12/20
to kivy-...@googlegroups.com

Here is a short example of how to put data into the TreeView at different levels.  This uses simple lists. 

The key concept to see is how the nodes attribute of the TreeView is used in the 3 methods of code view.

 

The example in the documentation shows how to store hierarchical data in a dict and use that to populate the tree.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.treeview import TreeView, TreeViewLabel


kv =
"""
<CodeTree>:
    text: 'Test Tree'
    root_options: {'text':'A Test of TreeView, this is the root node'}

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        size_hint_y: None
        height: dp(48)
        Button:
            text: '1 Add nodes'
            on_release: tv.add_codes()
        Button:
            text: '2 Add to code 300'
            on_release: tv.add_code_300()
        Button:
            text: '3 Add to code 300, 5'
            on_release: tv.add_code_l2('Code 300', 'Code L1: 5')
    CodeTree:
        id:tv
"""


class CodeTree(TreeView):
    codes = [
f'Code {str(i)}' for i in range(100, 600, 100)]  # These are used to 'simulate' your database output
   
codes_l1 = [f'Code L1: {str(i)}' for i in range(0, 20, 5)]
    codes_l2 = [
f'Code L2 {str(i)}' for i in range(0, 5)]

   
def add_codes(self):
       
for code in self.codes:
           
self.add_node(TreeViewLabel(text=code))  # top level nodes to the tree

   
def add_code_300(self):
       
for node in self.root.nodes:
           
if node.text == 'Code 300':
               
for item in self.codes_l1:
                   
self.add_node(TreeViewLabel(text=item), node)

   
def add_code_l2(self, level_1, level_2):
       
for node in self.root.nodes:
           
if node.text == level_1:
               
for node_2 in node.nodes:
                   
if node_2.text == level_2:
                        
for item in self.codes_l2:
                           
self.add_node(TreeViewLabel(text=item), node_2)


class TreeViewTestApp(App):
   
def build(self):
       
return Builder.load_string(kv)


TreeViewTestApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/99f7896d-f8eb-40dd-a292-3c744e523d85%40googlegroups.com.

 

Matheus Baumgarten

unread,
Feb 12, 2020, 7:52:04 PM2/12/20
to Kivy users support
Thank you guys! I've finally understood the Treeview structure.
Reply all
Reply to author
Forward
0 new messages