1from imagetools import get_png_info, open_png 2 3# Register PNG image decoder 4decoder = lv.img.decoder_create() 5decoder.info_cb = get_png_info 6decoder.open_cb = open_png 7 8# Create an image from the png file 9try: 10 with open('../../assets/imgbtn_left.png','rb') as f: 11 imgbtn_left_data = f.read() 12except: 13 print("Could not find imgbtn_left.png") 14 sys.exit() 15 16imgbtn_left_dsc = lv.img_dsc_t({ 17 'data_size': len(imgbtn_left_data), 18 'data': imgbtn_left_data 19}) 20 21try: 22 with open('../../assets/imgbtn_mid.png','rb') as f: 23 imgbtn_mid_data = f.read() 24except: 25 print("Could not find imgbtn_mid.png") 26 sys.exit() 27 28imgbtn_mid_dsc = lv.img_dsc_t({ 29 'data_size': len(imgbtn_mid_data), 30 'data': imgbtn_mid_data 31}) 32 33try: 34 with open('../../assets/imgbtn_right.png','rb') as f: 35 imgbtn_right_data = f.read() 36except: 37 print("Could not find imgbtn_right.png") 38 sys.exit() 39 40imgbtn_right_dsc = lv.img_dsc_t({ 41 'data_size': len(imgbtn_right_data), 42 'data': imgbtn_right_data 43}) 44 45# Create a transition animation on width transformation and recolor. 46tr_prop = [lv.STYLE.TRANSFORM_WIDTH, lv.STYLE.IMG_RECOLOR_OPA, 0] 47tr = lv.style_transition_dsc_t() 48tr.init(tr_prop, lv.anim_t.path_linear, 200, 0, None) 49 50style_def = lv.style_t() 51style_def.init() 52style_def.set_text_color(lv.color_white()) 53style_def.set_transition(tr) 54 55# Darken the button when pressed and make it wider 56style_pr = lv.style_t() 57style_pr.init() 58style_pr.set_img_recolor_opa(lv.OPA._30) 59style_pr.set_img_recolor(lv.color_black()) 60style_pr.set_transform_width(20) 61 62# Create an image button 63imgbtn1 = lv.imgbtn(lv.scr_act()) 64imgbtn1.set_src(lv.imgbtn.STATE.RELEASED, imgbtn_left_dsc, imgbtn_mid_dsc, imgbtn_right_dsc) 65imgbtn1.add_style(style_def, 0) 66imgbtn1.add_style(style_pr, lv.STATE.PRESSED) 67 68imgbtn1.align(lv.ALIGN.CENTER, 0, 0) 69 70# Create a label on the image button 71label = lv.label(imgbtn1) 72label.set_text("Button") 73label.align(lv.ALIGN.CENTER, 0, -4) 74 75