MantisIDE

Lesson 4: Buttons and events · Round 1.28" touch watch

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Lesson 4 - Buttons and events Goal: react to touch. Widgets send events (CLICKED, LONG_PRESSED, VALUE_CHANGED ...) and you attach a Python function with add_event_cb.

You’ll learn: CLICKED, LONG_PRESSED, callbacks

Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Learning roadmap, Level 2 · Widgets and touch. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.

# Lesson 4 - Buttons and events
# Goal: react to touch. Widgets send events (CLICKED, LONG_PRESSED, VALUE_CHANGED ...)
# and you attach a Python function with add_event_cb.
import lvgl as lv
import display

display.init()                     # start the display and touch (does nothing if already on)

BG = 0x05110A
LINE = 0x24452F
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C

FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}


def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
    """lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
    lb = lv.label(parent)
    lb.set_text(text)
    lb.set_style_text_color(lv.color_hex(color), 0)
    lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
    if zoom:
        lb.set_style_transform_scale(int(256 * zoom), 0)   # 256 = 100 %
        lb.update_layout()
        lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
        lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
    lb.align(align, int(x), int(y))
    return lb

scr = lv.screen_active()             # the screen LVGL is showing
scr.clean()                           # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(BG), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)

count = [0]
value = label(scr, "0", TEXT, 16, y=-40, zoom=2.5)
hint = label(scr, "tap +, hold to reset", MUTED, 12, y=70)


def make_button(text, x, color):
    b = lv.button(scr)
    b.set_size(70, 70)
    b.align(lv.ALIGN.CENTER, x, 25)
    b.set_style_radius(lv.RADIUS_CIRCLE, 0)
    b.set_style_bg_color(lv.color_hex(color), 0)
    lb = lv.label(b)
    lb.set_text(text)
    lb.center()
    return b

plus = make_button(lv.SYMBOL.PLUS, 42, ACCENT)
minus = make_button(lv.SYMBOL.MINUS, -42, LINE)


def show():
    value.set_text(str(count[0]))


def on_plus(e):
    count[0] += 1
    show()


def on_minus(e):
    count[0] -= 1
    show()


def on_reset(e):
    count[0] = 0
    show()
    hint.set_text("reset!")

plus.add_event_cb(on_plus, lv.EVENT.CLICKED, None)
minus.add_event_cb(on_minus, lv.EVENT.CLICKED, None)
plus.add_event_cb(on_reset, lv.EVENT.LONG_PRESSED, None)
print("Tap the buttons on the watch. Long-press + to reset.")
Loading post…

Dialog