2017-03-01 51 views
1

我使用按鈕來檢索使用filechooser選擇的某些文件夾的路徑。點擊按鈕時,我想更新標籤的文本,以便顯示選定的路徑。Kivy:單擊按鈕時如何更新標籤

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

回答

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