Labs / LVGL / 03 Quick Start Example - Control Section (Based on Web Simulator and LVGL v9.0)

VI. Switches

Author: Li Zisheng · Source: FreakStudio open documentation, v1.0 · License: CC BY 4.0

Contents

The following code implements an interactive switch control on a black background, whose core function is to respond to switch state changes in real time and output corresponding prompts:

import display_driver
import lvgl as lv

lv.init()

#Get the currently active screen object
scr = lv.screen_active()
#Set the screen background color to black
scr.set_style_bg_color(lv.color_hex(0x000000), 0)

#Create a switch
switch = lv.switch(scr)
#Set the size
switch.set_size(100, 50)
#Center it
switch.center()

#Interaction callback function
def switch_event_cb(e):
    if switch.has_state(lv.STATE.CHECKED):
        print("OPEN")
    else:
        print("CLOSE")
#Register the interaction callback function. For more states see the "class EVENT" section of api.py
switch.add_event_cb(switch_event_cb, lv.EVENT.VALUE_CHANGED, None)

# Load the screen
lv.screen_load(scr)
VI. Switches
VI. Switches

In the code above:

6.1 Switch Linkage Tag (Control Text Display / Color)

Added a label control, which synchronously modifies the label text and color when the switch is toggled, realizing "control linkage".

import display_driver
import lvgl as lv

lv.init()

scr = lv.screen_active()
scr.set_style_bg_color(lv.color_hex(0x000000), 0)

# Create a switch
switch = lv.switch(scr)
switch.set_size(100, 50)
switch.align(lv.ALIGN.CENTER, 0, -40)  # Move up to leave room for the label

# ========== New: create the linked label ==========
label = lv.label(scr)
label.set_text("Switch is CLOSE")
label.align(lv.ALIGN.CENTER, 0, 20)
# Label style (initially red)
style_label = lv.style_t()
style_label.init()
style_label.set_text_color(lv.color_hex(0xFF0000))
label.add_style(style_label, lv.STATE.DEFAULT)

# Modified callback: linked label
def switch_event_cb(e):
    if switch.has_state(lv.STATE.CHECKED):
        print("OPEN")
        label.set_text("Switch is OPEN")
        label.set_style_text_color(lv.color_hex(0x00FF00), lv.STATE.DEFAULT)  # Green
    else:
        print("CLOSE")
        label.set_text("Switch is CLOSE")
        label.set_style_text_color(lv.color_hex(0xFF0000), lv.STATE.DEFAULT)  # Red

switch.add_event_cb(switch_event_cb, lv.EVENT.VALUE_CHANGED, None)

lv.screen_load(scr)
VI. Switches
VI. Switches

6.2 Complete Switch Configuration (Track and Slider Styles)

The following code implements a switch control with customizable style, precise centering and interactive feedback on a black background:

import display_driver
import lvgl as lv

lv.init()

#Get the currently active screen object
scr = lv.screen_active()
#Set the screen background color to black
scr.set_style_bg_color(lv.color_hex(0x000000), 0)

# Create and initialize the style object
style_switch = lv.style_t()
style_switch.init()

#Create a switch
switch = lv.switch(scr)
#Set the size
switch.set_size(100, 50)

# First remove all default styles
switch.remove_style_all()

# Off state + track (main part): dark gray
switch.set_style_bg_color(lv.color_hex(0x444444), lv.STATE.DEFAULT | lv.PART.MAIN)
switch.set_style_bg_opa(255, lv.STATE.DEFAULT | lv.PART.MAIN)  # Opaque
switch.set_style_radius(25, lv.STATE.DEFAULT | lv.PART.MAIN)   # Rounded corners
switch.set_style_border_width(0, lv.STATE.DEFAULT | lv.PART.MAIN)  # Remove the default border

# On state + track (main part): green
switch.set_style_bg_color(lv.color_hex(0x00FF00), lv.STATE.CHECKED | lv.PART.MAIN)
switch.set_style_bg_opa(255, lv.STATE.CHECKED | lv.PART.MAIN)
switch.set_style_radius(25, lv.STATE.CHECKED | lv.PART.MAIN)
switch.set_style_border_width(0, lv.STATE.CHECKED | lv.PART.MAIN)

# Off state knob: white
switch.set_style_bg_color(lv.color_hex(0xFFFFFF), lv.STATE.DEFAULT | lv.PART.KNOB)
switch.set_style_bg_opa(255, lv.STATE.DEFAULT | lv.PART.KNOB)
switch.set_style_radius(25, lv.STATE.DEFAULT | lv.PART.KNOB)  # Round knob

# On state knob: yellow
switch.set_style_bg_color(lv.color_hex(0xFFFF00), lv.STATE.CHECKED | lv.PART.KNOB)
switch.set_style_bg_opa(255, lv.STATE.CHECKED | lv.PART.KNOB)
switch.set_style_radius(25, lv.STATE.CHECKED | lv.PART.KNOB)

# Center it
switch.center() 

#Interaction callback function
def switch_event_cb(e):
    if switch.has_state(lv.STATE.CHECKED):
        print("OPEN")
    else:
        print("CLOSE")

#Register the interaction callback function. For more states see the "class EVENT" section of api.py
switch.add_event_cb(switch_event_cb, lv.EVENT.VALUE_CHANGED, None)

# Load the screen
lv.screen_load(scr)
25px
0x00FF00 0xFFFF00

The core logic for customizing the styled state of the switch (track + slider) is as follows:

After all style and size settings are completed, call switch.center (): based on the final style and size state of the switch to recalculate the centering coordinates, so as to avoid position offset caused by premature centering and ensure that the switch is accurately displayed in the exact center of the screen.

Version Record

Version Number

Modifier

Time

Content

v1.0

Li Zisheng

2026/03/23

Initialize the document.