2017-03-01 7 views
1

J'utilise un bouton pour récupérer les chemins de certains dossiers sélectionnés avec le filechooser. Lorsque le bouton est cliqué, je voudrais mettre à jour le texte de l'étiquette afin qu'il affiche les chemins sélectionnés.Kivy: comment mettre à jour une étiquette quand on clique sur un bouton

Dans mon 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 

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

Cela ne me donne pas d'erreur, mais le texte de l'étiquette ne change pas.

J'ai également essayé self.ListViewFolder.text = b comme recommandé here mais j'obtiens cette erreur: MyWidget' object has no attribute 'Lb_ListViewFolder'.

Je l'ai vu answer, mais j'ai du mal à appliquer dans mon code

J'utilise python 3.6 et Kivy 1.9.2.dev0


Dans le cas, ceci est mon code entier:

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

Répondre

2

Vous pouvez utiliser StringProperty ici:

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

Ou vous pouvez le changer directement en 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