Kivy:单击按钮时如何更新标签

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

我使用一个按钮来检索用文件选择器选择的一些文件夹的路径。单击按钮后,我想更新标签的文本,以便显示选定的路径。

在我的 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

在 .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)

这没有给我任何错误,但标签文本没有改变。

我也像推荐的 here 一样尝试 self.ListViewFolder.text = b,但我收到此错误:MyWidget' object has no attribute 'Lb_ListViewFolder'

我看过这个answer,但我在代码中应用时遇到问题

我使用 python 3.6 和 Kivy 1.9.2.dev0


以防万一,这是我的全部代码:

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()

您可以在此处使用 StringProperty:

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()

或者直接在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