1

Good day,

I have a traditional Kivy viewclass "RecycleViewRow", which takes rv.data [{'id': "some_ID",'status': "Good, 'invoiceNumber': "1"},{'id': "some_other_ID",'status': "bad, 'invoiceNumber': "24"}]and with properly-named properties in both files ie. id = String Property(") and text: root.id produces a working RecycleView.

Working with different datasets - always list of dicts with identical key structures - I find myself copy-pasting viewclasses in both files and changing the differentkey = String Property(") and text: root.differentkey properties in the new, copy-pasted viewclasses. It works great, but the copy-paste method is repetitive.

So, curious then if there was a way of dynamically creating & removing Kivy Properties in the Viewclass, and binding them to widgets. This would all be done on the .py side, with a format_rows() function being called from whichever screen is active and just finished creating their rv.data. May be too bulky to be practical, but something in the vein of (but not):

class MyScreen(Screen):

    def __init__(self, **kwargs):
        self.rcvrx = RecycleViewRow()
        super(MyScreen, self).__init__(**kwargs)
    
    result = [{'id': "some_ID",'status': "Good, 'invoiceNumber': "1"},{'id': "some_other_ID",'status': "bad, 'invoiceNumber': "24"}]

    self.rcvrx.create_rows(result)


class RecycleViewRow(RecycleDataViewBehavior, BoxLayout):
    #id = StringProperty("")
    #status = StringProperty("")
    #invoiceNumber = StringProperty("")

    def __init__(self, **kwargs):
        super(RecycleViewRow, self).__init__(**kwargs)
        self.widgets_to_clear = []
        self.properties = {}

    def format_rows(self,data_list):  ### with rv.data passed from the call
        self.properties.clear() ###remove any existing properties
        [self.remove_widget(t) for t in self.widgets_to_clear] ###remove any existing widgets since the data may have changed
    
        for k in data_list:  ###ascertain the key structure of one dict of rv.data
            if len(k) > 0:
                il = k
                break

        RV().ids.the_box.cols = len(il.keys())  ### adjust RecycleBoxLayout (in the RV class, not included here) cols to befit the number of keys/properties
 

### 1. Create referable string property in the style of key = StringProperty() (identical function to original typing id = StringProperty())

### 2. Create Label

### 3. Bind StringProperty to Label.text

### 4. Add Widget to list (for removal at beginning of next run)

        for key in il.keys(): 

            self.properties[key] = StringProperty("")  ### this would require the RecycleDataViewBehavior to retrieve properties from the self.properties dict rather than self ...(?)
            w = Label(size_hint_x=0.1, halign='center',text_size=(None, None))
            self.add_widget(w)
            #self.bind(self.properties[key]=w.text) ### not understanding bind correctly in this context
            self.widgets_to_clear.append(w)

The achievement would be that you could use one Viewclass for multiple datasets which use the same RV class.

Thought going beyond the copy-paste routine and getting in the muck would help broaden my understanding of these things. Spent a while reading in the docs and on here but couldn't self-solve. Thanks in advance for sharing!

autopy
  • 11
  • 2

0 Answers0