lvgl / lv_binding_micropython

LVGL binding for MicroPython
MIT License
259 stars 166 forks source link

Getting and casting object when it is of a derived widget type #355

Open riaancillie opened 1 month ago

riaancillie commented 1 month ago

I have created a custom widget by deriving it from a base LVGL object

class SpinLabel(lv.label):

    def __init__(self, parent):
        super().__init__(parent)
        self.add_flag(lv.obj.FLAG.SCROLLABLE)
        self.set_user_data(self)

When trying to style the object in a custom theme's onApply callback is there any way to determine if the object is my custom widget and if so, how do I cast it to that object type so that I can access it's methods? So far, it seems only the base lv.label type is accessible and even if I check the id() of the created custom object vs the id() of the obj in the theme callback, the two IDs are different.

class sdLvglTheme(lv.theme_t):
....
def apply(self, theme, obj):

        #print(obj.get_class())    
        #print(obj.get_class() == lv.label_class)
        try:
            #SpinLabel.__cast__(obj).setValue(2)
            if (obj.get_class() == lv.label_class):
                s =  lv.label.__cast__(obj)
                s.set_text("2")
                print(s)
            #s.setValue(2)
            a = obj.get_user_data().__dereference__(3)
            print(bytes(a))            
        except Exception as e:
            print(e)