Trying to set background color on a label object.
Obviously don’t understand how it works.
mystyle = lv.style_t(lv.style_plain)
mystyle.body.main_color = lv.color_hex(0x000000) # background top color (main), 0xRRGGBB
mystyle.body.grad_color = lv.color_hex(0x880000) # background bottom color (gradient), 0xRRGGBB
mystyle.text.color = lv.color_hex(0xffffff) # text-colour, 0xRRGGBB
This works: (seeting the style for the scr
scr.set_style(mystyle)
but setting a style just for one label does not.
label = lv.label(btn)
label.set_text(“Button”)
label.set_style(mystyle)
I get an error: TypeError: argument num/types mismatch
Hi @Glenn_Meader!
According to the docs, lv_label_set_style
receives 3 parameters, the label, style type (should be LV_LABEL_STYLE_MAIN
) and a pointer to the style itself.
The docs were written for the C API, but the same applies to Micropython.
In case of Micropython the first parameter is “self” for object methods, and the function expects the other two parameters.
So it should be something like:
label.set_style(lv.label.STYLE.MAIN, mystyle)
Here’s how I got it to work:
You have to enable “draw” to change the background of a label with lbl.set_body_draw(lbl)
scr = lv.obj()
lbl = lv.label(scr)
lbl.set_text(" Label Text ")
lbl.set_body_draw(lbl)
mystyle = lv.style_t(lv.style_plain)
mystyle.body.main_color = lv.color_hex(0x000000) # background top color (main), 0xRRGGBB
mystyle.body.grad_color = lv.color_hex(0x880000) # background bottom color (gradient), 0xRRGGBB
mystyle.text.color = lv.color_hex(0xffff00) # text-colour, 0xRRGGBB
lbl.set_style(lv.label.STYLE.MAIN, mystyle)
lv.scr_load(scr)
1 Like
That’s correct, according to the documentation.