简体   繁体   中英

Can't get kivy label to update through variable

I'm trying to get an App working where I get a list of folders in a directory, displayed in kivy recycleview. I then want to select one of the Folders and have it's name showing up in a label. I'm new to kivy and fairly new to python and after over two days and reading posts several times over and over again, I just can't manage to get the selected folder being displayed in the label.I got it working if I called the function through a button in kv but not through the SelectableButton class.

My.py file:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, StringProperty, ObjectProperty, BooleanProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout


prjct_list_clean = ['Folder1', 'Folder2', 'Folder3', 'Folder4', 'Folder5', 'Folder6']

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    """ Adds selection and focus behaviour to the view. """
    selected_value = StringProperty('')
    btn_info = ListProperty(prjct_list_clean)

class SelectableButton(RecycleDataViewBehavior, Button, Widget):
    """ Add selection support to the Label """
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

#############################ProblemPart###################################

    def on_press(self):
        con_wksp = str(self.text)
        self.ids.con_wksp_label = con_wksp
        print(self.ids.con_wksp_label) #Value gets printet

###########################################################################

class RV(RecycleView):
    rv_layout = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = []
        for subject in prjct_list_clean:
            self.data.append({'text':subject})
        print(prjct_list_clean)

class SelectedWksp(Widget):
    pass

class TroubleshootApp(BoxLayout, App,):
    def build(self):
        self.add_widget(RV())
        self.add_widget(SelectedWksp())
        return self

if __name__ == "__main__":
    TroubleshootApp().run()

My.kv file:

#:kivy 2.0.0

<RV>:
    rv_layout: rv_layout
    bar_width: 0
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        id: rv_layout
        default_size: None, dp(56)
        default_size_hint: 0.9, None
        size_hint_y: None
        height: self.minimum_height
        orientation: "vertical"

<SelectedWksp>:
    BoxLayout:
        Label:
            id: con_wksp_label
            text: 'Default'

I'm sure it's probably some simple rookie mistake, but I just can't get my head around it.

Thank you for everyones time.

You can change your on_press() method to:

def on_press(self):
    App.get_running_app().set_label(self.text)

and modify your App class to:

class TroubleshootApp(BoxLayout, App,):
    def build(self):
        self.add_widget(RV())
        self.wksp = SelectedWksp()   # save a reference to the SelecetedWksp instance
        self.add_widget(self.wksp)
        return self

    def set_label(self, txt):   # set the text of the label
        self.wksp.ids.con_wksp_label.text = txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM