adzy2k6
adzy2k6

Reputation: 117

How can you access the on_press handler of a nested button?

I am currently designing a Kivy application, where I want to make as many of my components re-usable. I have some code which follows the following style.

<ListButtonGroup@BoxLayout>:
    orientation: 'vertical'
    spacing: 10

    up: up
    down: down
    list: list

    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1.0, None

        Button:
            id: up
            text: 'up'
            size_hint: 1.0, None
            height: 50
        Button:
            id: down
            text: 'down'
            size_hint: 1.0, None
            height: 50

    ScrollList:
        id: list
        size_hint: 1.0, 0.8

I want this class to be used in multiple places, but I can't figure out how to make it so that I can assign on_press handlers to these buttons to use the class in multiple different areas for varying purposes and functionality.

Upvotes: 0

Views: 41

Answers (1)

noEmbryo
noEmbryo

Reputation: 2231

You can use some properties for that like this:

<ListButtonGroup@BoxLayout>:
    orientation: 'vertical'
    spacing: 10

    up: up
    down: down
    list: list

    callback1: lambda: None
    callback2: lambda: None

    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1.0, None

        Button:
            id: up
            text: 'up'
            size_hint: 1.0, None
            height: 50
            on_press: root.callback1()
        Button:
            id: down
            text: 'down'
            size_hint: 1.0, None
            height: 50
            on_press: root.callback2()

    ScrollList:
        id: list
        size_hint: 1.0, 0.8

After that you just use:

ListButtonGroup
    callback1: lambda x: print("1")
    callback2: lambda x: print("2")

Upvotes: 2

Related Questions