Kivy ListView NamError

278 views Asked by At

I am trying to create a ListView in Kivy containing, potentially, very long strings. I would like for the Labels for these strings to wrap. I know how to make a Label wrap by setting:

        text_size: root.width, None
        size: self.texture_size

However, when I try setting this in the args converter:

args_converter = lambda row_index, rec: {'text': rec['text'],
                                         'size_hint_y': None,
                                         'text_size': (root.width, None),
                                         'size': self.texture_size}

list_adapter = ListAdapter(data=data,
                           args_converter=args_converter,
                           cls=ListItemLabel,
                           selection_mode='single',
                           allow_empty_selection=False)

list_view = ListView(adapter=list_adapter)

I get a NameError: name 'root' is not defined. Replacing the 'root.width' with a number, I also get a NameError for 'self'.

Thanks in advance for any help.

1

There are 1 answers

0
inclement On BEST ANSWER

Make your own ListItemLabel subclass in which you add the kv rules.

class YourLabel(ListItemLabel): pass

Then pass cls=YourLabel in the args converter.