Cameron Sima
Cameron Sima

Reputation: 5385

Binding on_press with events in Kivy

I'm having a hard time with Kivy's sytem of half pure-python and half kv language setup. All I'm trying to do is for now is a 'hello world' type on_press event, and I can't get it to work.

from kivy.uix.modalview import ModalView
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.app import App
import citylists
import cat_dict

from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.storage.jsonstore import JsonStore

store = JsonStore('data.json')

Builder.load_string("""
#:import ListItemButton kivy.uix.listview
#:import sla kivy.adapters.listadapter

<ListViewModal>:
    ListView:
        size_hint: .8, .8
        adapter:
            sla.ListAdapter(
            data=["{0}".format(i) for i in root.categories],
            on_press=root.callback(self),
            cls=ListItemButton.ListItemButton)

""")




class ListViewModal(ModalView):
    categories = sorted(cat_dict.SECTION_DICT)

    def __init__(self, **kwargs):
        super(ListViewModal, self).__init__(**kwargs)

    def callback(self, instance):
        print "HI" + str(instance)


class MainView(GridLayout):

    def __init__(self, **kwargs):
        kwargs['cols'] = 1
        super(MainView, self).__init__(**kwargs)

        listview_modal = ListViewModal()

        self.add_widget(listview_modal)

class MainScreen(Screen):
    pass


mainscreen=MainScreen()
mainlayout = MainView()
mainscreen.add_widget(mainlayout)

sm = ScreenManager()
sm.add_widget(mainscreen)

class CARApp(App):

    def build(self):
       return sm


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

cat_dict.py

SECTION_DICT = {
    "accounting+finance": "acc",
    "admin / office": "ofc",
    "arch / engineering": "egr",
    'art / media / design': 'med',
    'biotech / science': 'sci',
    'business / mgmt': 'bus',
    'customer management': 'csr',
    'education': 'edu',....

Ultimately, I want to bind the on_press event for each of the dynamically created buttons titled with each key in SECTION_DICT, then save the value in JsonStore.

In simple terms all I need to happen is for a user to press a button to choose a craigslist category, which will return the 3 letter abbreviation to be used later in the program.

Upvotes: 0

Views: 1711

Answers (1)

kitti
kitti

Reputation: 14814

A ListAdapter does not have an on_press event. You need to bind to the on_press event of each button, which can be done using an args converter:

#:import ListItemButton kivy.uix.listview.ListItemButton
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
<ListViewModal>:
    ListView:
        size_hint: .8, .8
        adapter:
            ListAdapter(
            data=["{0}".format(i) for i in root.categories],
            args_converter=lambda row_index, rec: \
            {'text': rec, 'on_press': root.callback, 'size_hint_y': None, 'height': 25},
            cls=ListItemButton)

Also, take care to pass functions themselves as callbacks rather than the return value of functions. In other words, use root.callback instead of root.callback(self).

Upvotes: 1

Related Questions