How to translate kv expressions into Python?

47 views
Skip to first unread message

Dietrich Bollmann

unread,
Nov 6, 2019, 4:28:01 PM11/6/19
to Kivy users support
Hi,

I am experimenting with the second example from page

  - RecycleView

In the example, the following class and kv specs are given:

class RV(RecycleView):

   
def __init__(self, **kwargs):
       
super(RV, self).__init__(**kwargs)
       
self.data = [{'text': str(x)} for x in range(100)]

<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
: True
        touch_multiselect
: True


I tried to translate the kv expressions into Python resulting in the following code:

class RV(RecycleView):
   
def __init__(self, **kwargs):
       
super(RV, self).__init__(**kwargs)

       
self.viewclass = SelectableLabel

        layout
= SelectableRecycleBoxLayout()
        layout
.default_size = None, 56
        layout
.default_size_hint = 1, None
        layout
.size_hint_y = None
        layout
.height = layout.minimum_height
        layout
.orientation = 'vertical'
        layout
.multiselect = True
        layout
.touch_multiselect = True
       
self.add_widget(layout)

       
self.data = [{'text': str(x)} for x in range(100)]

When executing the code no error is shown.  However, the box remains empty.

Any idea how to make the code run?

Thanks for your help,

Dietrich



example-kv.py
example-python.py

Robert Flatt

unread,
Nov 7, 2019, 12:16:04 PM11/7/19
to Kivy users support
This is a great question.

Andrei Sima

unread,
Nov 8, 2019, 1:30:05 AM11/8/19
to Kivy users support
In the second example, I do not see where you add RV to the app

do you:
[....imports]

class
RV(RecycleView): def __init__(self, **kwargs): super(RV, self).__init__(**kwargs)
        [.....your code here]


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

if __name__ == '__main__':
    TestApp().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/c4df5a91-f48a-4977-94db-b1e6586bde41%40googlegroups.com.

Andrei Sima

unread,
Nov 8, 2019, 1:42:02 AM11/8/19
to Kivy users support
Completed the code same behavior as described by Robert Flat.

code:

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


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


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:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))



class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)

        self.data = [{'text': str(x)} for x in range(100)]
        self.viewclass = SelectableLabel()

        layout = SelectableRecycleBoxLayout()
        layout.default_size = None, 56
        layout.default_size_hint = 1, None
        layout.size_hint_y = None
        layout.height = layout.minimum_height
        layout.orientation = 'vertical'
        layout.multiselect = True
        layout.touch_multiselect = True
        self.add_widget(layout)


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


if __name__ == '__main__':
    TestApp().run()

Dietrich Bollmann

unread,
Nov 11, 2019, 11:47:35 AM11/11/19
to Kivy users support
Hi Andrei,

Thank you very much for your answer!

Does the code work on your computer?

I tried it on mine - and the result is still the same:  A plain black window without the selectable items shown...

Regards, Dietrich

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Andrei Sima

unread,
Nov 11, 2019, 11:57:32 AM11/11/19
to Kivy users support
Same beautiful black window on my side also.
I did not use it like that. I only used it in KV language to spear me of these problems.


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/47bf3ffc-fdf9-426c-a149-de4ca3b0a3aa%40googlegroups.com.

Dietrich Bollmann

unread,
Nov 12, 2019, 8:28:13 AM11/12/19
to Kivy users support
:)

Even if seen from a declarative point of view the Python code looks like the equivalent of the KV statements, it seems that something else has to be done:  maybe some kind of ...update...() function etc.?

Or could it be that KV is more than an option?

Maybe in the kivy framework GUIs should ALWAYS be built with the aid of the KV language rather than Python?

Thanks for your help, Andrei & Greetings from Dietrich

Reply all
Reply to author
Forward
0 new messages