Recycleview problem with understanding(trying to pass data to a layout in recycleview)

30 views
Skip to first unread message

Big Red Eyes Productions

unread,
Dec 5, 2022, 3:39:41 AM12/5/22
to Kivy users support
I could easily be ignorant of the answer, in fact I claim to be so, but if you can help educate me on what I'm missing, I'll say thank you and offer a gold star.

Here's my kivy:
<ContactCard>:
    name: 'ContactCard'
    usrLst: usrLst
    pswLst: pswLst

    orientation: "vertical"
    BoxLayout:
        orientation: "horizontal"
        Label:
            id: usrLst
            text: "user"
            size_hint_y: None
        Label:
            id: pswLst
            text: "pass"
            size_hint_y: None
        Button:
            text: "Button"
            on_release: print(usrLst.text,'',pswLst.text)
            canvas:
                #Clear
    HSeparator:


<RV>:
    viewclass: 'ContactCard'
    RecycleBoxLayout:
        default_size: None, dp(60)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

Here's the python to go with it:
class ContactCard(BoxLayout):
    usrLst = StringProperty('user')
    pswLst = StringProperty('pass')

    def on_pre_enter(self, *args):
        self.usrLst.text = str(BBApp.search_results[0][0])
        self.pswLst.text = str(BBApp.search_results[0][1])


class RV(RecycleView):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.data = [{'usrLst': str(x),'pswLst': str(x)}\
             for x in range(12)]


What I'm trying to do is produce a recycle view that provides a boxlayout for its list items.

I'm calling from my sqlite database to match for contacts and trying to pass that data to the labels in the boxlayout.


Elliot Garbus

unread,
Dec 5, 2022, 7:38:18 AM12/5/22
to kivy-...@googlegroups.com

Here is an example of a Recycleview that uses a BoxLayout (TwoButtons) as the viewclass.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty

kv =
'''
<TwoButtons>:
# This class is used as the viewclass in the RecycleView
# The means this widget will be instanced to view one element of data from the data list.
# The RecycleView data list is a list of dictionaries.  The keys in the dictionary specify the
# attributes of the widget.
    Button:
       text: root.left_text
        on_release: print(f'Button {self.text} pressed')
    Button:
        text: root.right_text
        on_release: print(f'Button {self.text} pressed')

BoxLayout:
    orientation: 'vertical'
    Button:
        size_hint_y: None
        height: 48
        text: 'Add widget to RV list'
        on_release: rv.add()

    RV:                          # A Reycleview
        id: rv
        viewclass: 'TwoButtons'  # The view class is TwoButtons, defined above.
        data: self.rv_data_list  # the data is a list of dicts defined below in the RV class.
        scroll_type: ['bars', 'content']
        bar_width: 10
        RecycleBoxLayout:       
            # This layout is used to hold the Recycle widgets
            default_size: None, dp(48)   # This sets the height of the BoxLayout that holds a TwoButtons instance.

            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height   # To scroll you need to set the layout height.
            orientation: 'vertical'
'''


class TwoButtons(BoxLayout):  # The viewclass definitions, and property definitions.
   
left_text = StringProperty()
    right_text = StringProperty()


class RV(RecycleView):
    rv_data_list = ListProperty() 
# A list property is used to hold the data for the recycleview, see the kv code

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.rv_data_list = [{'left_text': f'Left {i}', 'right_text': f'Right {i}'} for i in range(2)]
       
# This list comprehension is used to create the data list for this simple example.
        # The data created looks like:
        # [{'left_text': 'Left 0', 'right_text': 'Right 0'}, {'left_text': 'Left 1', 'right_text': 'Right 1'},
        # {'left_text': 'Left 2', 'right_text': 'Right 2'}, {'left_text': 'Left 3'},...
        # notice the keys in the dictionary correspond to the kivy properties in the TwoButtons class.
        # The data needs to be in this kind of list of dictionary formats.  The RecycleView instances the
        # widgets, and populates them with data from this list.

   
def add(self):
        l =
len(self.rv_data_list)
       
self.rv_data_list.extend(
            [{
'left_text': f'Added Left {i}', 'right_text': f'Added Right {i}'} for i in range(l, l + 1)])


class RVTwoApp(App):

   
def build(self):
       
return Builder.load_string(kv)


RVTwoApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f6f8704-4db5-4883-914c-ebb6bbd5d4a4n%40googlegroups.com.

 

Big Red Eyes Productions

unread,
Dec 6, 2022, 1:05:36 AM12/6/22
to Kivy users support
Wow this is cool, thank you for your time!

I have ran your code and I'm reading the comments a few times to soak it in.

I'm guess that, in order to populate the texts with my search results from my database, I would  do something like the following:

rv .rv_data_list = [{'user_text': f' {i[0]}', 'pass_text': f'{i[1]}'} for i in search_results] (where search results is a tuple  generated by db query.

How can I update the values? Right now it's showing all blank. I want to pass my search results into the rv.data.

Big Red Eyes Productions

unread,
Dec 6, 2022, 1:18:12 AM12/6/22
to Kivy users support
I am trying to implement the add function you provided, however I'm getting an error.

'kivy.properties.ListProperty' object has no attribute 'extend'

Big Red Eyes Productions

unread,
Dec 6, 2022, 1:31:16 AM12/6/22
to Kivy users support
A little bit more tinkering and I got it worked out!

def search(self, text):
        self.search_results = self.dbConnect(action="select",\
            id=text, dbChoice=self.dbChoices['users'])
        print("Results: ",self.search_results)      
        print(self.search_results[0][1],self.search_results[0][2])
        RV.data = [
{'usrLst': f'{i[0]}', 'pswLst': f'{i[1]}'}\
    for i in self.search_results]
        return self.search_results

2022-12-05 22_29_47-BB.png


Thanks for helping!

Elliot Garbus

unread,
Dec 6, 2022, 8:21:13 AM12/6/22
to kivy-...@googlegroups.com

Congratulations!  Glad to hear you got it working.

 

From: Big Red Eyes Productions
Sent: Monday, December 5, 2022 11:31 PM
To: Kivy users support

Subject: Re: [kivy-users] Recycleview problem with understanding(trying topassdata to a layout in recycleview)

 

A little bit more tinkering and I got it worked out!

 

def search(self, text):

        self.search_results = self.dbConnect(action="select",\

            id=text, dbChoice=self.dbChoices['users'])

        print("Results: ",self.search_results)      

        print(self.search_results[0][1],self.search_results[0][2])

        RV.data = [

{'usrLst': f'{i[0]}', 'pswLst': f'{i[1]}'}\

    for i in self.search_results]

        return self.search_results

 

 

 

Thanks for helping!

 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.

2022-12-05 22_29_47-BB.png
Reply all
Reply to author
Forward
0 new messages