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]}'))
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).
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.