Search code examples
pythonkivytreeview

how to make the treeview label in kivy visible background color


I am trying to make my treeview label visible, I mean when I do tree view and run and expand the tree, I can see the problem is that other UI element like text input is visible and I don't want that and I can't make the selection if it's above text input

from kivy.uix.treeview import TreeViewLabel,TreeView, TreeViewNode
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class Accounting(App):
    def build(self):
        tree=TreeView(hide_root=True)
        root=tree.add_node(TreeViewLabel(text='choose'))
        for a in range(100):
            tree.add_node(TreeViewLabel(text=str(a),even_color=(0,0,0,0)),root)
        # if you try here to change the color visibility the numbers will appears but you can't select it
        textinput=TextInput(hint_text='textinput',foreground_color=(1,1,1,1),background_color=(0,0,0,0))
    
        layout=BoxLayout(orientation='vertical')
        layout.add_widget(tree)
        layout.add_widget(textinput)

        return layout

Accounting().run()

Solution

  • Try something like this. It is not necessary to hide the text field, but it looks better to me

    from kivy.app import App
    from kivy.uix.treeview import TreeViewLabel
    from kivy.lang import Builder
    
    KV = """
    FloatLayout:
        TextInput:
            id: text_field
            size_hint_y: None
            pos_hint: {'center_y': 0.5}
            height: self.minimum_height
            
        ScrollView:
            TreeView:
                id: tv
                size_hint_y: None
                height: self.minimum_height
                hide_root: True
                on_node_expand: app.hide(True)
                on_node_collapse: app.hide(False)
    """
    
    
    class Accounting(App):
        def build(self):
            return Builder.load_string(KV)
    
        def on_start(self):
            root = self.root.ids.tv.add_node(TreeViewLabel(text="Choose"))
            for a in range(100):
                self.root.ids.tv.add_node(TreeViewLabel(text=str(a)), root)
    
        def hide(self, hide):
            if hide:
                self.root.ids.text_field.foreground_color = (1, 1, 1, 1)
                self.root.ids.text_field.background_color = (0, 0, 0, 0)
            else:
                self.root.ids.text_field.foreground_color = (0, 0, 0, 1)
                self.root.ids.text_field.background_color = (1, 1, 1, 1)
    
    
    Accounting().run()