3

test.py

    import sqlite3 as lite

    from kivy.uix.screenmanager import Screen
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.core.window import Window
    from kivy.uix.popup import Popup
    from kivy.uix.treeview import TreeView, TreeViewLabel, TreeViewNode
    from kivy.uix.label import Label
    from kivy.properties import ObjectProperty

    Window.clearcolor = (0, 0.517, 0.705, 1)

    Window.size = (700, 530)

    con = lite.connect('fact.db')
    con.text_factory = str
    cur = con.cursor()


    def populate_tree_view(tree_view, parent, node):
        if parent is None:
            tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
                                                         is_open=True))
        else:
            tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
                                                         is_open=True), parent)

        for child_node in node['children']:
            populate_tree_view(tree_view, tree_node, child_node)



        rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]

    tree = [{'node_id': 'Test2',
             'children': []},
            {'node_id': 'Test3',
             'children': []}]


    class TreeViewLabel(Label, TreeViewNode):
        pass


    class TreeviewGroup(Popup):
        treeview = ObjectProperty(None)
        tv = ObjectProperty(None)

        def __init__(self, **kwargs):
            super(TreeviewGroup, self).__init__(**kwargs)
            self.tv = TreeView(root_options=dict(text="Test1"),
                               hide_root=False,
                               indent_level=4)
            for branch in tree:
                populate_tree_view(self.tv, None, branch)
            self.remove_widgets()
            self.treeview.add_widget(self.tv)

        def remove_widgets(self):
            for child in [child for child in self.treeview.children]:
                self.treeview.remove_widget(child)


    class GroupScreen(Screen):
        groupName = ObjectProperty(None)
        popup = ObjectProperty(None)

        def display_groups(self, instance):
            if len(instance.text) > 0:
                self.popup = TreeviewGroup()
                self.popup.open()


    class Group(App):

        rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]
        def build(self):
            self.root = Builder.load_file('test.kv')
            return self.root


    if __name__ == '__main__':
        Group().run()

test.kv

:kivy 1.10.0

<TreeViewLabel>:
    on_touch_down:
        app.root.stateName.text = self.text
        app.root.popup.dismiss()

<TreeviewGroup>:
    id: treeview
    treeview: treeview
    title: "Select City"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout
        orientation: "vertical"
        BoxLayout:
            id: treeview
        Button:
            size_hint: 1, 0.1
            text: "Close"
            on_release: root.dismiss()


<CustomLabel@Label>:
    text_size: self.size
    valign: "middle"
    padding_x: 5

<SingleLineTextInput@TextInput>:
    multiline: False

<GreenButton@Button>:
    background_color: 1, 1, 1, 1
    size_hint_y: None
    height: self.parent.height * 0.150

GroupScreen:
    stateName: stateName

    GridLayout:
        cols: 2
        padding : 30,30
        spacing: 10, 10
        row_default_height: '40dp'

        CustomLabel:
            text: 'State Name'

        SingleLineTextInput:
            id: stateName
            on_text: root.display_groups(self)

        CustomLabel:
            text: 'State Code'

        SingleLineTextInput:
            id: stateCode

        CustomLabel:
            text: 'City Name'

        SingleLineTextInput:
            id: cityName

        CustomLabel:
            text: 'Short Name'

        SingleLineTextInput:
            id: shortName

        CustomLabel:
            text: 'Pin Code'

        SingleLineTextInput:
            id: pinCode

        GreenButton:
            text: 'Ok'

        GreenButton:
            text: 'Cancel'

        Label:

        Label:

Can anyone help me?
1. How to add a search filter textBox up on 'Test1'.where we can search from List.If anyone type Test1 then only should show test1 from treeview.
2. Test1 behave like parent node of all node.how to remove this property.How to change all node to like parent
3. Now test1,test2,test3 are coming static.how to change into dynamic.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nirdesh Kumawat
  • 386
  • 2
  • 16
  • 56

1 Answers1

3
  1. Test1 behave like parent node of all node.how to remove this property.How to change all node to like parent

You can set Test1 as a child node then set the text property of the treeView root_options to ''

...

tree = [{'node_id': 'Test1',
        'children': []},
        {'node_id': 'Test2',
         'children': []},
        {'node_id': 'Test3',
         'children': []}]

...

class TreeviewGroup(Popup):
    treeview = ObjectProperty(None)
    tv = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(TreeviewGroup, self).__init__(**kwargs)
        self.tv = TreeView(root_options=dict(text=""),
                       hide_root=False,
                       indent_level=4)

...
  1. How to add a search filter textBox up on 'Test1'.where we can search from List.If anyone type Test1 then only should show test1 from treeview.

You can define a filter method on the popup to handle that, an example:

class TreeviewGroup(Popup):

...

def filter(self, f):
    self.treeview.clear_widgets()
    self.tv = TreeView(root_options=dict(text=""),
                       hide_root=False,
                       indent_level=4)
    new_tree = []
    for n in tree:
        if f.lower() in n['node_id'].lower():
            new_tree.append(n)
    for branch in new_tree:
        populate_tree_view(self.tv, None, branch)

    self.treeview.add_widget(self.tv)

then call it when the user have entered a significant state name:

...

class GroupScreen(Screen):
    groupName = ObjectProperty(None)
    popup = ObjectProperty(None)

    def display_groups(self, instance):
        if len(instance.text) > 4: #I choose 4 because it is the smallest length of your nodes
            if self.popup is None:
                self.popup = TreeviewGroup()
            self.popup.filter(instance.text)
            self.popup.open()

I hope this helps !

update : for your last request

  1. add the ti attribute to the popup (textinput):

    ... class TreeviewGroup(Popup): treeview = ObjectProperty(None) tv = ObjectProperty(None) ti = ObjectProperty() ...

then edit the .kv file:

...
<TreeviewGroup>:
    id: treeview
    treeview: treeview
    title: "Select City"
    ti: ti
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout
        orientation: "vertical"
        TextInput:
            id: ti
            size_hint_y: .1
            on_text: root.filter(self.text)
        BoxLayout:
            id: treeview
        Button:
            size_hint: 1, 0.1
            text: "Close"
            on_release: root.dismiss()
...

don't forget to edit the display_groups method:

...
class GroupScreen(Screen):
    groupName = ObjectProperty(None)
    popup = ObjectProperty(None)

    def display_groups(self, instance):
        if len(instance.text) > 0:
            if self.popup is None:
                self.popup = TreeviewGroup()
            self.popup.filter(instance.text)
            self.popup.open()

Update: change the tree to dynamic:

...

# cur.execute("SELECT * FROM `m_state` order by state_id asc")
# rows = cur.fetchall()
# print(rows)
rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)]

tree = []

for r in rows:
    tree.append({'node_id': r[1], 'children': []})

...
Simon Mengong
  • 2,625
  • 10
  • 22
  • can you help me?When i type something then don't type anything in state name textBox But select city pop up should be open and add a textBox(Filter Box) top on treeView root_options. where i can filter easily?If i type 'tes' Then Show all column which have 'tes' string in word.And i type 'test1' Then only test1 should b show – Nirdesh Kumawat Nov 11 '17 at 14:21
  • 1
    @yashkumar ok I will update my answer,. for the static to dinamic, I think you have just to set the tree variable – Simon Mengong Nov 11 '17 at 14:27
  • Now test1,test2,test3 are coming static.how to change static into dynamic.my data stored in rows = [(1, 'test1', 111), (2, 'test2', 112), (3, 'test3', 113), (4, 'test4', 114)] – Nirdesh Kumawat Nov 13 '17 at 06:19
  • @yashkumar you have just to edit the tree variable. I am adding that to my answer – Simon Mengong Nov 13 '17 at 06:24
  • can you more elaborate that how to change? – Nirdesh Kumawat Nov 13 '17 at 06:41
  • @yashkumar I've added it – Simon Mengong Nov 13 '17 at 06:50