That’s correct.
Here is a previous discussion on the subject:
- Do you want to attach some data to an LVGL object?
- Or do you want to pass context to a callback?
To pass some extra context to an event handler, the simplest way is to use a lambda as explained above. This allows you to pass any number of arguments to the event callback function (arguments which you set when you add the event callback).
If you want to “assign” some values to an LVGL object, the simplest way is to inherit from the LVGL object and add members to your derived objects. For a derived object to keep its identity you need to set user_data to point to the derived object instead of the base object.
For example:
class MyBtn(lv.btn):
def __init__(self, parent, additional_data):
super().__init__(parent)
self.additional_data = additional_data
self.set_user_data(self)
Then you can use btn.additional_data
anywhere, also in an event callbacks. (Online example)
(if you don’t set user_data
in MyBtn
constructor it will still work, but when calling evt.get_target()
you would get the parent lv.btn
instead of MyBtn
and you will not have access to your data members)
We use this technique today when creating custom widgets in Micropython: