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

时间:2017-03-01 13:17:25

标签: kivy kivy-language object-property

我使用按钮检索使用filechooser选择的某些文件夹的路径。单击按钮时,我想更新标签的文本,以便它显示所选路径。

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

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

我也尝试了self.ListViewFolder.text = b,例如推荐的here,但我收到此错误: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()

1 个答案:

答案 0 :(得分:2)

您可以在此处使用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