Working with pages of a Tabview

To teach myself how to use LVGL in micropython I’ve been porting over portions of the lv_widgets demo from C to Python. I’ve encountered small problem, though. When I add a page to a tabview, I get a generic lvgl obj as the type, not a lvgl.page object. In the C example code, there are calls such as this one (where tab_page is a lv_obj_t type) and it works fine:

lv_page_set_scrl_layout(tab_page, LV_LAYOUT_PRETTY_TOP);

In python this does not work. I tried:

lvgl.page.set_scrl_layout(tab_page, lvgl.LAYOUT.PRETTY_TOP)

and I get the exception TypeError: argument should be a ‘page’ not a 'obj

Yet this is exactly what is done in the C code. What am I missing here?

Hi @torriem!

The C prototype of lv_tabview_add_tab returns lv_obj_t, not a lv_page_t.
But if you need to access page specific functionality, you can cast it to a page like this:

tabs = lv.tabview(scr)
page1 = lv.page.__cast__(tabs.add_tab("page1"))
page1.set_scrl_layout(lv.LAYOUT.PRETTY_TOP)
1 Like

That works great. Thank you.

One more question… When processing events for a table in a callback, I don’t see any way to use the lv.table.get_pressed_cell(). Except for lists, objects are immutable, so I don’t see any way for this method to return the row and column to me. Apparently it takes 2 arguments (well 3 including the object itself) which are in C pointers. How do we do this in Python? I can verify if I pass None, None to it, I get a a return value of 1, which is lv.RES.OK. But that of course doesn’t get the row and col data to me.

LVGL Micropython binding provides a way to use C pointers.
You can do something like this:

row_ptr = lv.C_Pointer()
col_ptr = lv.C_Pointer()
res = obj.get_pressed_cell(row_ptr, col_ptr)
if res == lv.RES.OK:
    row, col = row_ptr.int_val, col_ptr.int_val
    ...
1 Like

Thank you. Is this documented anywhere on the website? Or by spending quality time in the REPL tab-completing lv and reading very carefully? I’ve managed to find most things so far that way. But not all.

You can find some information and explanation about C_Pointer in the blog.
You can also find C_Pointer in some example scripts.
But you are right, there should be better documentations.

On LVGL v.6 there are many Micropython examples on the LVGL docs. Almost every component has both C and Micropython example!
Unfortunately they were remove on v.7 docs, but there is an open issue to add them back.

And yes, REPL quality time is a great way to explore LVGL!

1 Like