I am trying to convert the list example from the lvgl documentation to python.
I defined an event handler as follows:
def event_handler(obj, event):
if event == lv.EVENT.CLICKED:
print(“Clicked: %s” % list_btn.get_btn_text(obj))
and I set it to the buttons:
list_btn.set_event_cb(event_handler)
Unfortunately this does not work because an lv.obj is passed into the event handler instead of lv.list and list_btn.get_btn_text(obj) complains
How do I do this correctly?
In my opinion, lv_list API is really a mess.
There are two problems:
There is no dedicated set_event_cb. It relies on inherited set_event_cb from lv_obj, therefore the event handler sees lv_obj argument instead of lv_list
The function lv_list_get_btn_text is a member of lv_list according to the naming convention, but it receives lvgl btn as first argument and not a list!
It’s still possible to use it in Micropython with some casting.
In the callback you actually need to cast the obj to list (although it really represents a button…) so the right C function can be called.
Thanks for the quick answer. I wanted to cast it to lv.list but I am not (yet) and expert in Python and I simply didn’t know how casting worked.
Your solution works fine!