[HELP]lv.list maybe have bugs

The simulator code works well.

lv.list simulator

But when I try it on esp32 with the blow code, I got this error:

image

scr = lv.scr_act()
list1 = lv.list(scr)
list1.set_size(320, 240)

def event_handler(obj, event):
    if event is lv.EVENT.CLICKED:
        print(lv.list.get_btn_text(obj))  # this obj argument has wrong type.

list_btn = list1.add_btn(lv.SYMBOL.IMAGE, "main.py")
list_btn.set_event_cb(event_handler)
list_btn = list1.add_btn(lv.SYMBOL.FILE, "test.py")
list_btn.set_event_cb(event_handler)
list_btn = list1.add_btn(lv.SYMBOL.FILE, "app.py")
list_btn.set_event_cb(event_handler)
list_btn = list1.add_btn(lv.SYMBOL.FILE, "temp.py")
list_btn.set_event_cb(event_handler)

lv.scr_load(scr)

The source code:

/*
 * lvgl extension definition for:
 * const char *lv_list_get_btn_text(const lv_obj_t *btn)
 */
 
STATIC mp_obj_t mp_lv_list_get_btn_text(size_t mp_n_args, const mp_obj_t *mp_args)
{
    const lv_obj_t *btn = mp_to_lv(mp_args[0]);
    const char * _res = lv_list_get_btn_text(btn);
    return convert_to_str((void*)_res);
}

STATIC MP_DEFINE_CONST_LV_FUN_OBJ_VAR(mp_lv_list_get_btn_text_obj, 1, mp_lv_list_get_btn_text, lv_list_get_btn_text);

I think this is another bug or I used it in wrong way? :upside_down_face:

My guess would be that ESP32 is using an older version of the binding that has this problem, and the relatively newer binding in the simulator has fixed it?

What happens if you add print(obj) before your other print call? What object type gets printed? I see lvgl obj, and things work as I’d expect.

Yes, it was lvgl obj.
image

But I am use the lv_bindings master branch to build it. :thinking:

Hi @imliubo!

There is a bug here, but it’s in LVGL API itself.
lv_list_get_btn_text is defined like this:

This definition violates the convention for a member function.
The Micropython bindings expect to find list object as the first argument of any lv_list_* function. However, in this case a btn is received instead of a list.

A correct version of this function would be something like:

const char * lv_btn_get_text(const lv_obj_t * btn);

because this is really a member function of btn, not of list.

@embeddedt / @kisvegabor - would you consider moving this function to btn class? Is there any reason it’s part of lv_list_* namespace?
There may be other cases like this in lvgl, here is a related one.

Anyway, for v6 and v7 it’s not possible to change this due to API backward compatibility.
Instead, you can tell Micropython to cast the btn object to list manually with the __cast__ function, like this:

lv.list.__cast__(obj).get_btn_text()

Online example

I’ve tested this example on both the simulator and on ESP32

It’s in the lv_list namespace as it only applies to buttons created directly by the list object. A workaround would be to rename the function to lv_list_btn_get_text and have it accept a list object as the first parameter (though it would be ignored).

OK, I solved it by get the list index. :grin:

It works, I will try use it in my projects! :hugs:

Share my recently works with LVGL! :blush:

LVGL Projetct Video

1 Like

This will be a good solution.It’s great to be able to get the text content of the list directly. :wink: