Pass values from dictionary to RecycleView / button

Skip to first unread message

Salix plan

unread,
Feb 4, 2022, 9:12:47 AM2/4/22
to Kivy users support
Full code is in main.py.
This is not my code.

Q: How to pass Y (value from dictionary) to def btn_callback? See picture.
    Key from dict. (X) is displayed as button label. That is OK.

Screenshot_1.jpg

Thanks.

main.py

Elliot Garbus

unread,
Feb 4, 2022, 10:16:06 AM2/4/22
to kivy-...@googlegroups.com

I’m not sure what you’re trying to do.  Here is a more typical example of using a recycle view and accessing the other elements in the data list.  I hope this helps.

 

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import StringProperty, NumericProperty

KV =
"""
<CustomButton>:
    text: self.model
    on_release:
        print(f'The {self.model} from {self.brand} was introduced in {self.year}')
        app.root.ids.label.text = f'The {self.model} from {self.brand} was introduced in {self.year}'


<RV>:
    viewclass: 'CustomButton' # defines the viewtype for the data items.
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical' # defines the orientation of data items

BoxLayout:   # The root widget
    RV:
    Label:
        id: label
        text: 'Select a Car Model'
        text_size: self.size
        valign: 'center'
        halign: 'center'
"""


class CustomButton(Button):
    brand = StringProperty()
    model = StringProperty()
    year = NumericProperty()


class RV(RecycleView):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.data = [{"brand": "Ford", "model": "Mustang", "year": 1964},
                     {
"brand": "Chevrolet", "model": "Corvette", "year": 1953},
                     {
"brand": "Ferrari", "model": "Dino", "year": 1957}] + \
                    [{
'brand': f'brand {i}', 'model': f'model {i}', 'year': 1960 + i} for i in range(30)]


class SampleApp(App):
   
def build(self):
       
return Builder.load_string(KV)


SampleApp().run()

 

 

 

 

 

From: Salix plan
Sent: Friday, February 4, 2022 7:12 AM
To: Kivy users support
Subject: [kivy-users] Pass values from dictionary to RecycleView / button

 

Full code is in main.py.

This is not my code.

 

Q: How to pass Y (value from dictionary) to def btn_callback? See picture.

    Key from dict. (X) is displayed as button label. That is OK.

 

 

Thanks.

 

--
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/8ea57636-8206-42e3-9fbb-2f8f58efd821n%40googlegroups.com.

 

Screenshot_1.jpg

Salix plan

unread,
Feb 4, 2022, 2:28:04 PM2/4/22
to Kivy users support
Yes, something like this. But I am "blind" to use your code with mine :(

I will try to explain again:

If I run my main.py I got this:

Screenshot_18.jpg

Buttons labels are OK.
If I push 1. button (brand), function btn_callback prints brand , and I want to print ford
It is important that it is printed via the function, like  btn_callback.

main.py is not my code, but I am using it to show my problem.

My real code has ScreenManager and when I see your example at first glance, everything seems clear to me.
But if I want to implement your code with mine I got stuck.

Maybe to explain in a different way. I have a dictionary.
The keys of that dictionary form buttons (see picure above)
 and when the button is clicked, the values of the dictionary should be printed via the function.

I hope I didn’t complicate things too much :) 

Thanks in advance.


Elliot Garbus

unread,
Feb 4, 2022, 5:00:50 PM2/4/22
to kivy-...@googlegroups.com

The benefit of a recycleview, is that if you have a large number of buttons it only instances the widgets that can be seen, and “recycles” them.  The viewclass uses a list of dictionaries to associate properties in the viewclass with the keys in the dictionary.  Take a look at the example I shared, you can see the keys of the data list are the names of the properties.   These properties are used in both the display of the data and the printing the data.

 

In the code below, I’ve modified the example to do as you have requested.  I have a callback local the CustomButton, and a similar callback in RV.    I would put this callback in CustomButton.  Notice how they are called in the kv code.  For this example the root_object property is not used.  It does not need to go in the list of dicts, or be created as a property.  I have left it in place in case you have some other need for it.   Within CustomButton the rv can be accessed as self.parent.parent.

 

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import ObjectProperty

KV =
"""
<CustomButton>:
    on_release:
        self.local_btn_callback()  # using a local method, defined in CustomButton
        app.root.ids.rv.rv_btn_callback(self.value)  # using a method define in RV
        app.root.ids.label.text = f'The {self.text} value is {self.value}'



<RV>:
    viewclass: 'CustomButton' # defines the viewtype for the data items.
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical' # defines the orientation of data items

BoxLayout:   # The root widget
    RV:
        id: rv
    Label:
        id: label
        text: 'Select a Button'

        text_size: self.size
        valign: 'center'
        halign: 'center'
"""


class CustomButton(Button):
    root_widget = ObjectProperty() 
# there is no need for this property here or in the list of dicts
   
value = ObjectProperty()

   
def local_btn_callback(self):
       
print(f'Local_btn_callback: {self.value}')


class RV(RecycleView):
   
def __init__(self, **kwargs):
       
super().__init__
(**kwargs)
        dict_x = {
"brand": "Ford", "model": "Mustang", "year": 1964}
       
self.data = [{'text': str(x), 'root_widget': self, 'value': y} for x, y in dict_x.items()]

   
def rv_btn_callback(self, y):
       
print(f'rv_btn_callback: {y}')


class SampleApp(App):
   
def build(self):
       
return Builder.load_string(KV)


SampleApp().run()

From: Salix plan
Sent: Friday, February 4, 2022 12:28 PM
To: Kivy users support

Subject: Re: [kivy-users] Pass values from dictionary to RecycleView / button

 

Yes, something like this. But I am "blind" to use your code with mine :(

 

I will try to explain again:

 

If I run my main.py I got this:

 

 

Buttons labels are OK.

If I push 1. button (brand), function btn_callback prints brand , and I want to print ford

It is important that it is printed via the function, like  btn_callback.

 

main.py is not my code, but I am using it to show my problem.

 

My real code has ScreenManager and when I see your example at first glance, everything seems clear to me.

But if I want to implement your code with mine I got stuck.

 

Maybe to explain in a different way. I have a dictionary.

The keys of that dictionary form buttons (see picure above)

 and when the button is clicked, the values of the dictionary should be printed via the function.

 

I hope I didn’t complicate things too much :) 

 

Thanks in advance.

 

--

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.

Screenshot_18.jpg

Salix plan

unread,
Feb 5, 2022, 4:08:14 PM2/5/22
to Kivy users support
thank you. it was instructive and useful


Reply all
Reply to author
Forward
0 new messages