Over writting Button Label

I have a Button1 (using EEZ Studio to create gui and stm32IDE)

image

I am trying to just change the buttons label from 0 to 1

But when I write the new label it puts it in the upper corner of the button and does not remove the 0.

image

I am using the function to change the label

void lv_label_set_text(lv_obj_t * obj, const char * text)

but I am not sure if I am supposed to call with lv_label_create? as the button is already created. But if I don’t use lv_label_create then the “1” does not appear at all. Also, I am using the objects.Button1 as the obj. I assume this is the correct obj that was created at init?

 void action_change_main_screen(lv_event_t * e)
 {
	
		 lv_label_set_text(lv_label_create(objects.Button1), "1");
	 
}

Hello,

This behaves as expected. lv_label_create(lv_obj_t* parent) will create a new object on top of the specified parent. So in your case here you have just created a new object as a child of your Button1 object and it appears on top. lv_label_create() then returns the created child and you immediately set the text.

Can you show how you create this button? It will probably look something like this:

lv_obj_t* button = lv_button_create(screen);
lv_obj_t* label = lv_label_create(button);

You will need to use this label object which is a child of your button and set the text of that. If you do not need to change anything about your Button1 object, perhaps you could store the label inside of objects struct(?) instead of the button itself.

Kind regards

The EEZ studio initializes and creates screen and buttons:

void create_screen_main() {
    lv_obj_t *obj = lv_obj_create(0);
    objects.main = obj;
    lv_obj_set_pos(obj, 0, 0);
    lv_obj_set_size(obj, 480, 272);
    {
        lv_obj_t *parent_obj = obj;
        {
            lv_obj_t *obj = lv_label_create(parent_obj);
            lv_obj_set_pos(obj, 356, 232);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, "Hello, world!");
        }
        {
            // button1
            lv_obj_t *obj = lv_btn_create(parent_obj);
            objects.button1 = obj;
            lv_obj_set_pos(obj, 62, 86);
            lv_obj_set_size(obj, 100, 50);
            lv_obj_add_event_cb(obj, action_change_keypad_screen, LV_EVENT_PRESSED, (void *)10);
            {
                lv_obj_t *parent_obj = obj;
                {
                    lv_obj_t *obj = lv_label_create(parent_obj);
                    lv_obj_set_pos(obj, 0, 0);
                    lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
                    lv_label_set_text(obj, "0");
                    lv_obj_set_style_align(obj, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
                }
            }
        }
        {
            // button2
            lv_obj_t *obj = lv_btn_create(parent_obj);
            objects.button2 = obj;
            lv_obj_set_pos(obj, 275, 94);
            lv_obj_set_size(obj, 100, 50);
            lv_obj_add_event_cb(obj, action_change_keypad_screen, LV_EVENT_PRESSED, (void *)1);
            {
                lv_obj_t *parent_obj = obj;
                {
                    lv_obj_t *obj = lv_label_create(parent_obj);
                    lv_obj_set_pos(obj, 0, 0);
                    lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
                    lv_label_set_text(obj, "0");
                    lv_obj_set_style_align(obj, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
                }
            }
        }
    }
}

So when I call with a new label

lv_label_set_text(objects.button1, label);

shouldn’t I be referencing the parent button “objects.button1”? Why is the label not getting updated?

Because you are referencing the parent button, as you say, but the label is a child of this button and your child does not “inheret” that function from its parent. You will have to call lv_label_set_text on the label object itself somehow. This might be quite slow but it will work:


lv_obj_t* label = lv_obj_get_child(objects.button1, 0);
lv_label_set_text(label, "my text");

OR if you want to be more safe try using lv_obj_get_child_by_type() instead.

Thank you!