Search code examples
kivypopup

Popup fails depending on what I BIND


Here is the relevant portion of my Animal Picker in KIVY file shown below:

AniPic@ANIMALPICKER id: aroot labelText: '' imageSource: '' animalCode: 'AB' animalName: 'GOGGA'

BoxLayout:
    orientation: 'horizontal'
    width: root.width
    pos: 0,0

    canvas.before:
        RoundedRectangle:
            pos: 5, 5
            # the next two lines determine the position of the rectangle for the image
            size: root.width-10,  root.height-10
            source: root.imageSource
            radius:[10]

    PinButton:
        id: _pin
        on_release:
            root.pin_action(root.labelText)

    Label:
        id: _label
        text: root.labelText
        width: root.width
        color: (1, 1, 1, 1)

    MapButton:
        id: _map
        on_release:
            root.map_show(root.labelText)

#==========================================================================

AnimalWindow:

: id: animalid name: 'animal'

canvas.before:
    Color:
        rgba: 1, 1, 1, 1

    Rectangle:
        pos: self.pos
        size: self.size

BoxLayout:
    orientation: 'vertical'

    Label:
        id: choice
        text: "Sightings"
        color: 0, 0, 0, 1
        size_hint_y: 0.05

        canvas.before:
            Color:
                rgba: 0.5, 1, 0.5, 1

            Rectangle:
                pos: self.pos
                size: self.size

    ScrollView:
        do_scroll_x: False
        do_scroll_y: True

        GridLayout:
            size: (root.width, root.height)
            cols: 1
            rows: 9
            padding: 10
            spacing: 10
            size_hint_x: None
            size_hint_y: 9
            height: self.minimum_height
            row_default_height: 120
            row_force_default: True

            AniPic:
                labelText: 'LION'
                imageSource: 'images/lion_pic.jpg'
                animalCode: 'LI'

            AniPic:
                labelText: 'CHEETAH'
                imageSource: 'images/cheetah_pic.png'
                animalCode: 'CH'

Now when I click on a 'PinButton' of a particular animal (CHEETA) I get the app executing

def pin_it(_animal): print('aaaa', _animal )

and shows.

'aaaa CHEETAH'

BUT the POPUP code below, where I want 'confirmation' for pin action, does not show the popup but crashes:

class ANIMALPICKER(RelativeLayout): def pin_action(self, _animal): # Prepare to confirm that you want to Pin the sighting title_text = _animal + ' ' + 'SIGHTING' content_text = 'Confirm Sighting?'

    content_box = BoxLayout(orientation='vertical')
    btn_box = BoxLayout(orientation='horizontal')
    btn_box.height = 24
    content_label = Label()
    content_label.text = content_text

    yes_btn = Button(text='Yes')
    yes_btn.background_color = 0, 1, 0, 1
    no_btn = Button(text='No')
    no_btn.background_color = 1, 0, 0, 1

    btn_box.add_widget(yes_btn)
    btn_box.add_widget(no_btn)

    content_box.add_widget(content_label)
    content_box.add_widget(btn_box)

    # Now confirm that you want to Pin the sighting
    popup = Popup(title=title_text,
                  separator_height=4,
                  title_size='20sp',
                  content=content_box,
                  size_hint=(None, None),
                  size=(200, 240),
                  auto_dismiss=False)

    # dismiss popup  and proceed to pin the sighting, on release of yes button
    yes_btn.bind(on_press=pin_it(_animal))
    # dismiss popup on release of the NO button
    yes_btn.bind(on_release=popup.dismiss)

    popup.open()

with the following message:

File "/home/sib/PycharmProjects/SpotMap/animalWindow.py", line 66, in pin_action yes_btn.bind(on_press=pin_it(_animal)) File "kivy/_event.pyx", line 444, in kivy._event.EventDispatcher.bind AssertionError: None is not callable

However when I bind a simple 'yes_btn.bind(on_press=dismiss)' instead of 'yes_btn.bind(on_press=pin_it(_animal))' the popup does display and the the popup dismisses when the button to which it is attached is pressed.

Please at 82 years of age and a limited knowledge of Python, Kivy and OO I desperately need help in completing this app.

A work around will also satisfy me

Many Thanks


Solution

  • I think the problem is that pin_it(_animal) completes the function instead of referencing the function object.

    from functools import partial
    myfun = partial(pin_it, animal=_animal)
    # this should be a function, not function()
    yes_btn.bind(on_press=myfun)