How to rewrite the label of a button?


how to rewrite the label of a button?

What MCU/Processor/Board and compiler are you using?

What do you want to achieve?

I create a button ,and add a label “button A” to it, then I want to rename the label when I clicked the button, and turn to “btn B”

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

## Screenshot and/or video
If possible, add screenshots and/or videos about the current state.
      lv_obj_t* btn = lv_btn_create(lv_scr_act(),NULL);
        lv_obj_t* label = lv_label_create(btn, NULL);
        lv_label_set_text(label, "ButtonA");
          btn->user_data = (void*) label;
          lv_obj_set_event_cb(btn, [](lv_obj_t*btn, lv_event_t event){
            if(event != LV_EVENT_CLICKED) return;
            lv_label_set_text((lv_obj_t*)btn->user_data, "ButtonB");

Note that you should almost never modify a structure variable of a LittlevGL object, and instead use the appropriate helper function (lv_obj_set/get_user_data).

@TridentTD’s solution should work. Another option is to get the button’s first child object. But this requires the button to only have a label as its child.