How to update a label's value ot a button's event

I am using the normal demo button code:

static void btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * btn = lv_event_get_target(e);
    if(code == LV_EVENT_CLICKED) {
      
        cnt++;

        /*Get the first child of the button which is the label and change its text*/
        lv_obj_t * label = lv_obj_get_child(btn, 0);
        lv_label_set_text_fmt(label, "Button: %d", cnt);
    }
}

Also, i have a working label as following (inside setup part):

lv_obj_t  * label1 = lv_label_create( lv_scr_act() );
  lv_label_set_text(label1, "test");
    lv_obj_set_pos(label1,  20, 20);

And i have moved the variable “static uint8_t cnt = 0;” outside void in order to make it global, so i have it as:
uint8_t cnt = 0;

Finally, what i want to do is to update label1 with the value of “cnt” when button is pressed.

I have tested many things as adding:
lv_label_set_text_fmt(label1, cnt);
or
lv_obj_t * label1_ = lv_obj_get_child(label1, 0);
lv_label_set_text_fmt(label1_, cnt);

The button is being updated but the label does not.

So, what am i doing wrong?
Any suggestions?

Thank you in advance!

Your label belongs to the active screen, not the button, so you cannot lv_obj_get_child from the button. Change to

lv_obj_t  * label1 = lv_label_create( btn );

Thank you very much for your reply.

The label does get updated now, BUT only being located inside the button.
It can not be somewhere outside the button.

(The final goal is to have 2 buttons and a label. The label will display the temperature. The buttons (the one with be +, the other will be -) will increase-decrease the value of temperature.)

Any further suggestion?
(i am using v8.3)

You can declare the 2 buttons and label as global, use these pointers directly, no need to use lv_obj_get_child(). And of course create thse lv objs with lv_scr_act() .

Declaring the label as global worked!

Thank you very much!