Code:

# 4x4portrait_buttonlayout_demo.py
import appuifw, graphics, e32, key_codes
RGB_GREEN = (000,255,000)
RGB_RED = (255,000,000)
RGB_BLACK = (000,000,000)
appuifw.app.directional_pad = False
appuifw.app.orientation='portrait'
appuifw.app.screen='large'
appuifw.app.body = canvas = appuifw.Canvas()
Total_x, Total_y = canvas.size
x=y=90
def handle_redraw(rect):
pass
appuifw.app.body=canvas=appuifw.Canvas(redraw_callback=handle_redraw)
img=graphics.Image.new(canvas.size)
yy = 220
xy_01 = ((x*0,y*0+yy), (x*1,y*1+yy))
xy_02 = ((x*1,y*0+yy), (x*2,y*1+yy))
xy_03 = ((x*2,y*0+yy), (x*3,y*1+yy))
xy_04 = ((x*3,y*0+yy), (x*4,y*1+yy))
xy_05 = ((x*0,y*1+yy), (x*1,y*2+yy))
xy_06 = ((x*1,y*1+yy), (x*2,y*2+yy))
xy_07 = ((x*2,y*1+yy), (x*3,y*2+yy))
xy_08 = ((x*3,y*1+yy), (x*4,y*2+yy))
xy_09 = ((x*0,y*2+yy), (x*1,y*3+yy))
xy_10 = ((x*1,y*2+yy), (x*2,y*3+yy))
xy_11 = ((x*2,y*2+yy), (x*3,y*3+yy))
xy_12 = ((x*3,y*2+yy), (x*4,y*3+yy))
xy_13 = ((x*0,y*3+yy), (x*1,y*4+yy))
xy_14 = ((x*1,y*3+yy), (x*2,y*4+yy))
xy_15 = ((x*2,y*3+yy), (x*3,y*4+yy))
xy_16 = ((x*3,y*3+yy), (x*4,y*4+yy))
def up_event(event):
global xy
img.clear()
img.rectangle(((xy)), fill=RGB_RED, width=5)
canvas.blit(img)
xy=None
def dn_event(event):
pass
canvas.bind(key_codes.EButton1Up, up_event, ((x*0,y*0), (Total_x,Total_y)))
canvas.bind(key_codes.EButton1Down, dn_event, ((0,0), (0,0)))
def box_01(event):
global xy
xy=xy_01
def box_02(event):
global xy
xy=xy_02
def box_03(event):
global xy
xy=xy_03
def box_04(event):
global xy
xy=xy_04
def box_05(event):
global xy
xy=xy_05
def box_06(event):
global xy
xy=xy_06
def box_07(event):
global xy
xy=xy_07
def box_08(event):
global xy
xy=xy_08
def box_09(event):
global xy
xy=xy_09
def box_10(event):
global xy
xy=xy_10
def box_11(event):
global xy
xy=xy_11
def box_12(event):
global xy
xy=xy_12
def box_13(event):
global xy
xy=xy_13
def box_14(event):
global xy
xy=xy_14
def box_15(event):
global xy
xy=xy_15
def box_16(event):
global xy
xy=xy_16
canvas.bind(key_codes.EDrag, box_01, ((xy_01)))
canvas.bind(key_codes.EDrag, box_02, ((xy_02)))
canvas.bind(key_codes.EDrag, box_03, ((xy_03)))
canvas.bind(key_codes.EDrag, box_04, ((xy_04)))
canvas.bind(key_codes.EDrag, box_05, ((xy_05)))
canvas.bind(key_codes.EDrag, box_06, ((xy_06)))
canvas.bind(key_codes.EDrag, box_07, ((xy_07)))
canvas.bind(key_codes.EDrag, box_08, ((xy_08)))
canvas.bind(key_codes.EDrag, box_09, ((xy_09)))
canvas.bind(key_codes.EDrag, box_10, ((xy_10)))
canvas.bind(key_codes.EDrag, box_11, ((xy_11)))
canvas.bind(key_codes.EDrag, box_12, ((xy_12)))
canvas.bind(key_codes.EDrag, box_13, ((xy_13)))
canvas.bind(key_codes.EDrag, box_14, ((xy_14)))
canvas.bind(key_codes.EDrag, box_15, ((xy_15)))
canvas.bind(key_codes.EDrag, box_16, ((xy_16)))
def exit():
global process
process = 0
app_lock.signal()
appuifw.app.exit_key_handler=exit
app_lock=e32.Ao_lock()
xy =None
prev_xy=None
img.rectangle(((xy_16)), fill=RGB_RED, width=5)
canvas.blit(img)
process = 1
while process:
if xy <> prev_xy and xy <> None:
prev_xy=xy
img.clear()
img.rectangle((xy), fill=RGB_GREEN, width=5)
canvas.blit(img)
e32.ao_yield() # bottom of _running_loop_
app_lock.wait()