I strongly advise against using user_data
argument in Micropython for that purpose.
user_data
is used internally by the Micropython Bindings.
You should keep the user_data
argument as None
in 99% of the cases.
It should only be used in cases where the caller on the C side needs to examine the user_data
and extract some information from it, which is never the case on LVGL callbacks.
You can easily achieve what you want very nicely in other ways.
Remember that in Python, as opposed to C, you can pass any Callable Object as a callback and not just plain functions. For example, you can pass an object’s member function or a lambda function.
So here is your example restrctured, trying to make it more Object Oriented.
No globals, with member functions and lambdas as callbacks:
Online simulator
# Initialize
import display_driver
import lvgl as lv
# --------------------------------------------------------------------------------
class MyMsgbox(lv.msgbox):
btns = ["YES", "NO", ""]
def __init__(self, yes_cb, txt):
super().__init__(None, "Confirm", txt, MyMsgbox.btns, True)
self.yes_cb = yes_cb
if self.yes_cb:
self.add_event_cb(self.msgbox_cb, lv.EVENT.VALUE_CHANGED, None)
def msgbox_cb(self, event):
txt = self.get_active_btn_text()
if txt == 'YES':
self.yes_cb()
self.close()
class MyBtn(lv.btn):
def __init__(self, parent, *msg_box_args):
super().__init__(parent)
self.msg_box_args = msg_box_args
if self.msg_box_args :
self.add_event_cb(self.btn_cb, lv.EVENT.CLICKED, None)
def btn_cb(self, event):
self.mbox = MyMsgbox(*self.msg_box_args)
self.mbox.center()
self.set_style_bg_color(lv.palette_main(lv.PALETTE.RED), 0)
# --------------------------------------------------------------------------------
modal = lv.obj()
# --------------------------------------------------------------------------------
mute_btn = MyBtn(modal)
mute_btn.set_size(200,40)
mute_btn.align(lv.ALIGN.TOP_RIGHT, -130, 60)
mute_btn_lbl = lv.label(mute_btn)
mute_btn_lbl.set_text("MUTE ALARM")
mute_btn_lbl.align(lv.ALIGN.CENTER, 0, 0)
# --------------------------------------------------------------------------------
mm_btn = MyBtn(modal, lambda: print('yes reset'), "Continue Reset of\nMinimum and Maximum\nTemperature Values ?")
mm_btn.set_size(200,40)
mm_btn.align(lv.ALIGN.TOP_RIGHT, -130, 120)
mm_btn_lbl = lv.label(mm_btn)
mm_btn_lbl.set_text("RESET MIN/MAX")
mm_btn_lbl.align(lv.ALIGN.CENTER, 0, 0)
# --------------------------------------------------------------------------------
fw_btn = MyBtn(modal, lambda: print('yes install'), "Update Found...\n Install ?")
fw_btn.set_size(200,40)
fw_btn.align(lv.ALIGN.TOP_RIGHT, -130, 180)
fw_btn_lbl = lv.label(fw_btn)
fw_btn_lbl.set_text("CHECK FOR UPDATE")
fw_btn_lbl.align(lv.ALIGN.CENTER, 0, 0)
# --------------------------------------------------------------------------------
pwr_btn = MyBtn(modal, lambda: print('yes power down'), "Are you shure you want to power down ?")
pwr_btn.set_size(200,40)
pwr_btn.align(lv.ALIGN.TOP_RIGHT, -130, 240)
pwr_btn_lbl = lv.label(pwr_btn)
pwr_btn_lbl.set_text("POWER OFF")
pwr_btn_lbl.align(lv.ALIGN.CENTER, 0, 0)
# --------------------------------------------------------------------------------
close_btn = lv.btn(modal)
close_btn.set_size(20,20)
close_btn.align(lv.ALIGN.CENTER, 96, -140)
close_btn_lblb = lv.label(close_btn)
close_btn_lblb.set_size(9,16)
close_btn_lblb.set_text("X")
close_btn_lblb.align(lv.ALIGN.CENTER, 0, -2)
lv.scr_load(modal)