kivykivy-languageobject-property

Kivy: how to update a label when a button is clicked


I use a button to retrieve the paths of some folders selected with the filechooser. When the button is clicked I would like to update the text of the label so that it dispays the selected paths.

In my Kv:

Button:
    text:'OK'
    on_press: root.selected(filechooser.path, filechooser.selection)
Label:
    id: Lb_ListViewFolder
    text: root.Lb_ListViewFolder_text
    color: 0, 0, 0, 1
    size_hint_x: .75

In .py:

class MyWidget(BoxLayout):

    Lb_ListViewFolder_text = ObjectProperty("Text")
    def selected(self, a, b):
        global Lb_ListViewFolder_text
        Lb_ListViewFolder_text = b
        print(a,b)

This doesn't give me any error but the label text isn't changed.

I also tried self.ListViewFolder.text = b like recommended here but I get this error: MyWidget' object has no attribute 'Lb_ListViewFolder'.

I have seen this answer, but I have trouble applying in my code

I use python 3.6 and Kivy 1.9.2.dev0


In case, this is my entire code:

from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.event import EventDispatcher

from kivy.lang import Builder
root = Builder.load_string('''
<MyWidget>
    id: BL_Main
    orientation: "horizontal"
    padding: 10
    spacing: 10
    BoxLayout:
        id: BL_folder  
        orientation: "vertical"
        Button:
            id:ok
            text:'OK'
            background_color: 0,0,1,1
            height: 5
            size_hint: 0.1, 0.1
            on_press: root.selected(filechooser.path, filechooser.selection)
        BoxLayout:
            orientation:"horizontal"
            size_hint: None, 0.9
            width:150
            canvas.before:
                Color:
                    rgb: .4,.5,.5
                Rectangle: 
                    pos: self.pos
                    size: self.size

            ## multiple select folder  not possible with FileChooserListView 
            FileChooserIconView:  
                id: filechooser
                pos:self.pos
                multiselect: True
                dirselect: True

    Label:
        id: Lb_ListViewFolder
        text: root.Lb_ListViewFolder_text
        color: 0, 0, 0, 1
        size_hint_x: .75


''')

class MyWidget(BoxLayout):

    Lb_ListViewFolder_text = ObjectProperty("Text")
    def selected(self, a, b):
        global Lb_ListViewFolder_text
        Lb_ListViewFolder_text = b
        print(a,b)


class MyApp(App):
    def build(self):
        Window.clearcolor = (1, 1, 1, 1)
        return MyWidget()

MyApp().run()

Solution

  • You can use StringProperty here:

    from kivy.app import App
    from kivy.uix.filechooser import FileChooserListView
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    from kivy.properties import StringProperty
    
    Builder.load_string('''
    
    <MyLayout>:
        orientation: "vertical"
        Label:
            text: root.label_text
        Button:
            id:ok
            text:'OK'
            on_press: root.selected(filechooser.path, filechooser.selection)
        FileChooserIconView:  
            id: filechooser
            pos:self.pos
            multiselect: True
            dirselect: True
    
    ''')
    
    
    class MyLayout(BoxLayout):
        label_text = StringProperty("File name")
    
        def selected(self, a, b):
            self.label_text = b[0]
    
    
    class MyApp(App):
    
        def build(self):
            return MyLayout()
    
    
    MyApp().run()
    

    Or you can change it directly in kvlang:

    <MyLayout>:
        orientation: "vertical"
        Label:
            id: dirlabel
            text: root.label_text
        Button:
            id:ok
            text:'OK'
            on_press: dirlabel.text = filechooser.selection[0]
        FileChooserIconView:  
            id: filechooser
            pos:self.pos
            multiselect: True
            dirselect: True