Initializing a list view on startup

23 views
Skip to first unread message

Al Dante

unread,
Dec 28, 2020, 12:20:22 PM12/28/20
to Kivy users support
Hi, 

I have very little experience with Kivy and my apologies if this is a newbie question. 

I am writing a Cocktails app, using Screens to guide users in their choices. E.g the first screen has buttons allowing the user to select following screens containing gin cocktails, rum cocktails, or to pick by flavour or type. 

If the user selects Gin cocktails, the next screen shows a single-selectable list of gin cocktails.  

How do I populate this list on entry to the screen?

The original uses a button, which when pressed, fills the list entries. As my list is static, I would like to avoid this. All the methods I have tried fail because the widget data property is not initialized at that point and is set to "None". 

I am using the SelectableLabel / SelectableBoxLayout / AddLocationForm from here: Kivy: Unknown class <ListView> error code but updated for Kivy 2.0. 

Many thanks in advance for your help.


Elliot Garbus

unread,
Dec 28, 2020, 6:21:18 PM12/28/20
to kivy-...@googlegroups.com

Screens have events that you can use.

I suggest you use on_pre_enter:

This will fire prior the animation of the screen transition.

https://kivy.org/doc/stable-1.11.1/api-kivy.uix.screenmanager.html?highlight=screenmanager#kivy.uix.screenmanager.Screen

--
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/f7f29105-9de5-4e24-be19-0be114090831o%40googlegroups.com.

 

victo...@googlemail.com

unread,
Dec 29, 2020, 4:03:48 AM12/29/20
to Kivy users support
Thank you very much - I will try that.

victo...@googlemail.com

unread,
Dec 29, 2020, 4:25:28 AM12/29/20
to Kivy users support
Thank you - that works perfectly.

victo...@googlemail.com

unread,
Dec 29, 2020, 4:37:11 AM12/29/20
to Kivy users support
class CocktailsList(BoxLayout):
    selectable_cocktails = ObjectProperty()


class CocktailsScreen(Screen):
    cocktails_list = ObjectProperty()

    def on_pre_enter(self, *args):
    print("Screen pre-enter")
    self.populate()

    form = None
    for child in self.children:
        if 'selectable_cocktails_list' in child.ids:
            # Copy cocktails into List widget
            child.selectable_cocktails.data = [{'text': x['name.text']} for x in self.cocktails_list]
            break

--------------------------- kv
<CocktailsScreen>:
    name: "cocktails"
    CocktailsList:

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

<CocktailsList>:
    orientation: "vertical"
    id: fred

    # The label (selectable_cocktails) must match the variable defined in the AddLocationForm class
    # The value (selectable_cocktails_list) must match the id of the RecycleView defined below
    selectable_cocktails: selectable_cocktails_list

    RecycleView:
        id: selectable_cocktails_list
        name: "rvcocktailslist"

        viewclass: 'SelectableLabel'

        SelectableRecycleBoxLayout:
            default_size: None, dp(26)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
            multiselect: False
            touch_multiselect: False
Reply all
Reply to author
Forward
0 new messages