1def ta_event_cb(e): 2 ta = e.get_target() 3 txt = ta.get_text() 4 # print(txt) 5 pos = ta.get_cursor_pos() 6 # print("cursor pos: ",pos) 7 # find position of ":" in text 8 colon_pos= txt.find(":") 9 # if there are more than 2 digits before the colon, remove the last one entered 10 if colon_pos == 3: 11 ta.del_char() 12 if colon_pos != -1: 13 # if there are more than 3 digits after the ":" remove the last one entered 14 rest = txt[colon_pos:] 15 if len(rest) > 3: 16 ta.del_char() 17 18 if len(txt) < 2: 19 return 20 if ":" in txt: 21 return 22 if txt[0] >= '0' and txt[0] <= '9' and \ 23 txt[1] >= '0' and txt[1] <= '9': 24 if len(txt) == 2 or txt[2] != ':' : 25 ta.set_cursor_pos(2) 26 ta.add_char(ord(':')) 27# 28# Automatically format text like a clock. E.g. "12:34" 29# Add the ':' automatically 30# 31# Create the text area 32 33LV_HOR_RES = lv.scr_act().get_disp().driver.hor_res 34LV_VER_RES = lv.scr_act().get_disp().driver.ver_res 35 36ta = lv.textarea(lv.scr_act()) 37ta.add_event_cb(ta_event_cb, lv.EVENT.VALUE_CHANGED, None) 38ta.set_accepted_chars("0123456789:") 39ta.set_max_length(5) 40ta.set_one_line(True) 41ta.set_text("") 42ta.add_state(lv.STATE.FOCUSED) 43 44# Create a keyboard 45kb = lv.keyboard(lv.scr_act()) 46kb.set_size(LV_HOR_RES, LV_VER_RES // 2) 47kb.set_mode(lv.keyboard.MODE.NUMBER) 48kb.set_textarea(ta) 49 50 51