Jump to content
Nokioteca Forum

Pygreco!


Ale152
 Share

Recommended Posts

Mi si era scaricata la pila della calcolatrice e per rimediare mi sono scritto questa in Python:

25161497.jpg50312933.jpg

Potrebbe avere qualche bug, è poco commentata e il codice potrebbe essere molto poco comprensibile.

L'ho scritta con la filosofia "basta che funziona", quindi non vi impressionate ;)

Se vi dovesse interessare potrei continuarne lo sviluppo :thumbs:

Sono ben accetti i commenti :thumbs:

import e32
import appuifw
import key_codes as key

calc = []
result = u''
FONT = ('normal', 22)
x = 5
shift = False
ht = 40 # altezza tasto
wt = 55 # larghezza tasto
keyboard = u'123456789S0,'
keyboard_s = u'sin,cos,tan,sqrt,^2,^-1,log,*10^,exp,S,pi,ANS'.split(',')
KEY = ('normal', 20)
JOYP = (200, 110)
JOYP_FONT = ('normal', 20)
JOY_W = 20
JOY_H = 20
JOY_C = (0, 200, 200)
RED = (200, 0 ,0)

def draw(r=None):
global shift, x
c.clear((0, 0, 0))
# Background
c.rectangle((0, 0, c.size[0], 55), fill = (0, 200, 200))
# Numeri
k = 0
for row in range(4):
	for col in range(3):
		rect = (wt*col, 65+ht*row, wt*col+wt, 65+ht*row+ht)
		c.rectangle(rect, fill = (255,255,255), outline=(0,0,0))
		if shift:
			if row == 3 and col == 0:
				c.rectangle(rect, fill = (255,0,0), outline=(0,0,0))
			tasto = (wt*col+wt/2-c.measure_text(keyboard_s[k], font = KEY)[1]/2, 65+ht*row+30)
			c.text(tasto, keyboard_s[k], font = KEY)
		else:
			tasto = (wt*col+wt/2-c.measure_text(keyboard[k], font = KEY)[1]/2, 65+ht*row+30)
			c.text(tasto, keyboard[k], font = KEY)
		k += 1
# Display
box = c.measure_text(u'%s =' % ''.join(calc), font = FONT)
x = min(max(20-box[1], x), 5)
c.text((x, 20), u'%s =' % ''.join(calc), font = FONT)
c.text((5, 45), result, font = FONT)
# Joypad
c.ellipse((JOYP[0]-JOY_W-10,
		   JOYP[1]-JOY_H-20,
		   JOYP[0]+JOY_W+20,
		   JOYP[1]+JOY_H+5),
		   fill = JOY_C)
if shift:
	c.text((JOYP[0], JOYP[1]-JOY_H), u'(', font = JOYP_FONT, fill = RED)
	c.text((JOYP[0], JOYP[1]+JOY_H), u')', font = JOYP_FONT, fill = RED)
	c.text((JOYP[0]+JOY_W, JOYP[1]), u'>', font = JOYP_FONT, fill = RED)
	c.text((JOYP[0]-JOY_W, JOYP[1]), u'<', font = JOYP_FONT, fill = RED)
else:
	c.text((JOYP[0], JOYP[1]-JOY_H), u'+', font = JOYP_FONT, fill = RED)
	c.text((JOYP[0], JOYP[1]+JOY_H), u'-', font = JOYP_FONT, fill = RED)
	c.text((JOYP[0]+JOY_W, JOYP[1]), u'*', font = JOYP_FONT, fill = RED)
	c.text((JOYP[0]-JOY_W, JOYP[1]), u'/', font = JOYP_FONT, fill = RED)	
c.text(JOYP, u'=', font = JOYP_FONT, fill = RED)

def key_handler(event):
global calc, result, shift, x
# Funzioni SHIFT
if shift == True:
	if event['keycode'] == key.EKeyUpArrow:
		calc.append(u'(')
	if event['keycode'] == key.EKeyDownArrow:
		calc.append(u')')
	if event['keycode'] == key.EKeyStar:
		shift = False
	if event['keycode'] == key.EKeyHash:
		calc = [result]
		shift = False
	if event['keycode'] == key.EKeyRightArrow:
		x -= 15
	if event['keycode'] == key.EKeyLeftArrow:
		x += 15
	if event['keycode'] == key.EKey0:
		calc.append(u'pi')
	if event['keycode'] == key.EKey1:
		calc.append(u'sin(')
	if event['keycode'] == key.EKey2:
		calc.append(u'cos(')
	if event['keycode'] == key.EKey3:
		calc.append(u'tan(')
	if event['keycode'] == key.EKey4:
		calc.append(u'sqrt(')
	if event['keycode'] == key.EKey5:
		calc.append(u'**2')
	if event['keycode'] == key.EKey6:
		calc.append(u'**(-1)')
	if event['keycode'] == key.EKey7:
		calc.append(u'log(')
	if event['keycode'] == key.EKey8:
		calc.append(u'*10**(')
	if event['keycode'] == key.EKey9:
		calc.append(u'exp(')	
# Funzioni non SHIFT
else:
	if event['keycode'] == key.EKeyStar:
		shift = True
	if event['keycode'] == key.EKeyUpArrow:
		calc.append(u'+')
	if event['keycode'] == key.EKeyDownArrow:
		calc.append(u'-')
	if event['keycode'] == key.EKeyRightArrow:
		calc.append(u'*')
	if event['keycode'] == key.EKeyLeftArrow:
		calc.append(u'/')
	if event['keycode'] == key.EKeyHash:
		calc[-1] += u'.'
	if event['keycode'] == key.EKeyStar:
		shift = True  
	if event['keycode'] in range(key.EKey0, key.EKey9+1):
		try:
			float(calc[-1])
			calc[-1] += u'%s' % (event['keycode'] - key.EKey0)
		except:
			calc.append(u'%s' % (event['keycode'] - key.EKey0))
# Funzioni globali
if event['keycode'] == key.EKeySelect:
	temp = []
	for c in calc:	
		try:
			float(c)
			temp.append('float(%s)' % c)
		except:
			temp.append(c)		
	try:
		result = str(eval(''.join(temp))).decode('utf-8')
	except:
		result = u'Errore di sintassi!'
if event['keycode'] == key.EKeyBackspace:
	if calc:
		if len(calc[-1]) > 1:
			calc[-1] = calc[-1][:-1]
		else:
			calc.pop()		   
draw()

def clear():
global calc, result
calc = []
result = u''
#lock.signal()

c = appuifw.Canvas(event_callback=key_handler, redraw_callback=draw)
appuifw.app.body = c
appuifw.app.exit_key_handler = clear
lock = e32.Ao_lock()
lock.wait()

Link to comment
Condividi su altri siti

Please sign in to comment

You will be able to leave a comment after signing in



Accedi Ora
 Share

×
×
  • Crea Nuovo...

Informazione Importante

Questo sito utilizza i cookie per analisi, contenuti personalizzati e pubblicità. Continuando la navigazione, accetti l'utilizzo dei cookie da parte nostra | Privacy Policy