How to get text from button


how to get text from button in another function

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


What LVGL version are you using?


What do you want to achieve?

get text from button

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:

void buttons_create_back(lv_style_t style_status_sec_bar_bg,lv_obj_t * sb, char * name, int* x_pos, int * y_pos, int* rad )
        lv_obj_t * btn_label;
        lv_obj_t * btn1 = lv_btn_create(sb, NULL);
        static lv_style_t style_btn_bg;
        static lv_style_t style_btn_pr_bg;
        lv_style_copy(&style_btn_bg, &style_status_sec_bar_bg);
        style_btn_bg.body.main_color = LV_COLOR_DEEPSKYBLUE;
        style_btn_bg.body.grad_color = LV_COLOR_DEEPSKYBLUE;
        style_btn_bg.text.color = LV_COLOR_WHITE;
        style_btn_bg.text.font = &lv_font_roboto_16;
        style_btn_bg.body.opa = 0;
        style_btn_bg.body.radius = rad;
        lv_style_copy(&style_btn_pr_bg, &style_btn_bg);
        style_btn_pr_bg.body.main_color = LV_COLOR_BUTTON_PR_GRAY;
        style_btn_pr_bg.body.grad_color = LV_COLOR_BUTTON_PR_GRAY;
        lv_btn_set_style(btn1,LV_BTN_STYLE_REL, &style_btn_bg);
        lv_btn_set_style(btn1,LV_BTN_STYLE_PR, &style_btn_pr_bg);
        lv_obj_set_event_cb(btn1, list_btn_event_handler);
        lv_obj_set_size(btn1, 50, 50);
        lv_obj_set_pos(btn1, x_pos,y_pos);
        btn_label = lv_label_create(btn1, NULL);
        lv_label_set_text(btn_label, name);
        lv_obj_set_auto_realign(btn_label, true);
        lv_obj_align(btn_label, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
        lv_label_set_align(btn_label, LV_LABEL_ALIGN_CENTER);

static void list_btn_event_handler(lv_obj_t * btn, lv_event_t event){
    if(event == LV_EVENT_CLICKED) {
        buf12 =lv_label_get_text(btn);


looks like crazy…

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

You can’t call lv_label functions on a button; you’ll need to retrieve the label object using lv_obj_get_child(btn, NULL), and then use that.

1 Like