官方博客:Micropython + LittlevGL | LVGL’s Blog
github:GitHub - lvgl/lv_micropython: Micropython bindings to LVGL for Embedded devices, Unix and JavaScript
官方在线模拟器:https://sim.lvgl.io/(需要电脑能访问外网才能使用)
电脑不能访问外网会出现以下错误:
1、在界面上创建一个按钮
Micropython代码:
# Initializeimport display_driver
import lvgl as lv# Create a button with a labelscr = lv.obj()
btn = lv.button(scr)
btn.align(lv.ALIGN.CENTER, 0, 0)
label = lv.label(btn)
label.set_text('Hello World!')
lv.screen_load(scr)
运行效果:
2、添加点击事件响应
Micropython代码:
# Initializeimport display_driver
import lvgl as lv# Create a button with a labelscr = lv.obj()
btn = lv.button(scr)
btn.align(lv.ALIGN.CENTER, 0, 0)
label = lv.label(btn)
label.set_text('Button')# define callback function
def btn_event_cb(e):code = e.get_code()if code == lv.EVENT.CLICKED:print("Button clicked!")btn.add_event_cb(btn_event_cb, lv.EVENT.ALL, None)lv.screen_load(scr)
运行效果:
鼠标左键点击Button,控制台会打印"Button clicked!"