Python Forum
[Kivy] Need to render updated recycler view
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] Need to render updated recycler view
#1
hello, i'm trying to learn kivy. I need to update one part of the screen based on interaction in another part.
Specifically, i need to get on_press input from a recycleView on left half of a boxLayout and update the recycleView on the right side of the boxLayout. It isnt working.

So far my code:
import openpyxl
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.boxlayout import BoxLayout

Builder.load_string('''


<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.5, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<SelectableLabel2>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.1, .5, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RootWid>:
    orientation: 'vertical'
    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'StuCheck'
                with_previous: False
            ActionOverflow:
            ActionButton:
                text: 'Populate list'
                on_press: QuestionsList().populate()
            ActionButton:
                text: 'Sort list'
                on_press: root.sort()
            ActionButton:
                text: 'Clear list'
                on_press: root.clear()
            ActionGroup:
                text: 'More'
                ActionButton:
                    text: 'Insert new item'
                    on_press: root.insert(new_item_input.text)
                ActionButton:
                    text: 'Update first item'
                    on_press: root.update(update_item_input.text)
                ActionButton:
                    text: 'Remove first item'
                    on_press: root.remove()
    BoxLayout:
        orientation: 'horizontal'
        RV:
        QuestionsList:

<RV>:
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: False
        touch_multiselect: True

<QuestionsList>:
    viewclass: 'SelectableLabel2'
    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: False
        touch_multiselect: True
''')


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''

class RootWid(BoxLayout):
    pass

class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)


    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            QuestionsList().populate(rv.data[index]['text'])
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))

class SelectableLabel2(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        print (data, 'refreshed')
        return super(SelectableLabel2, self).refresh_view_attrs(
            rv, index, data)


    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))


class QuestionsList(RecycleView):
    def __init__(self, **kwargs):
        super(QuestionsList, self).__init__(**kwargs)
    
    def clear(self):
        self.data = []

    def populate(self, subject):
        wb = openpyxl.load_workbook('sWL.xlsx')
        sheet = wb[subject]
        self.data = []
        for row in range (2, sheet.max_row+1):
            question = sheet.cell(row=row, column=1).value
            self.data.append({'text' : question})
        RootWid()    
        

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        subjects = [some list]
        self.data = []
        for subject in subjects:
            self.data.append({'text':subject})


class TestApp(App):
    def build(self):
        return RootWid()

if __name__ == '__main__':
    TestApp().run()
Please advice. Thanks in advance :)
Reply


Messages In This Thread
Need to render updated recycler view - by test - Nov-22-2018, 11:31 AM
RE: Need to render updated recycler view - by test - Nov-22-2018, 05:14 PM
RE: Need to render updated recycler view - by test - Nov-23-2018, 07:19 AM
RE: Need to render updated recycler view - by test - Nov-23-2018, 07:20 PM
RE: Need to render updated recycler view - by test - Nov-24-2018, 05:11 PM
RE: Need to render updated recycler view - by test - Nov-24-2018, 06:23 PM
RE: Need to render updated recycler view - by test - Nov-25-2018, 05:25 AM
RE: Need to render updated recycler view - by test - Nov-25-2018, 10:27 AM
RE: Need to render updated recycler view - by test - Nov-25-2018, 11:35 AM
RE: Need to render updated recycler view - by test - Nov-26-2018, 01:35 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020