-
Notifications
You must be signed in to change notification settings - Fork 1
/
pycalc_units.py
executable file
·175 lines (153 loc) · 5 KB
/
pycalc_units.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# Python calculator for conversion to SI units
import pygame
from math import *
BACKGROUND = 255, 255, 255
BORDER = 2
RES = 600,495 # initial window size
HSIZE, VSIZE = 6, 9 # button grid dimension (top row is for display)
MAXLEN = 100 # maximum digits
# font name
fn = "fsex2.ttf"
us_units = (
("_floz", "*0.0295735295625"), # l
("_pt", "*0.473176473"), # l
("_qt", "*0.946352946"), # l
("_gal", "*3.785411784"), # l
("_nm", "*1852"), # m
("_in", "*0.0254"), # m
("_ft", "*0.3048"), # m
("_sqft", "*0.09290304"), # m²
("_acre", "*4046.8564224"), # m²
("_yd", "*0.9144"), # m
("_mi", "*1609.344"), # m
("_lb", "*0.45359237"), # kg
)
# button layout
bmap = """_in _ft _yd _mi ** C
_gal exp( sin( cos( tan( log10(
_pt pi abs( **2 sqrt( log(
_qt deg( SCI ( ) /
_floz rad( 7 8 9 *
_sqft fah( 4 5 6 -
_acre cel( 1 2 3 +
_nm _lb 0 . j ="""
# color indices for buttons
col_ind = """333312
311111
311111
311114
315554
315554
315554
335554"""
col_ind = col_ind.splitlines()
# RGB colors for each color index
cols = (
(255, 255, 255),
(255,96,117),
(255,237,83),
(142,148,255),
(228,142,215),
(159,229,110),
)
# text colors and sizes
button_rgb = 0, 0, 0
display_rgb = 0, 0, 0
size_button = 30
size_disp = 55
but = []
for l in bmap.splitlines():
bb = l.split()
but.append(bb)
def deg(x):
return degrees(x)
def rad(x):
return radians(x)
def fah(x):
# Celsius -> Fahrenheit
return (x * 9/5) + 32
def cel(x):
# Fahrenheit -> Celsius
return (x - 32) * 5/9
class PyCalc:
def __init__(self):
pygame.init()
self.res = RES
self.screen = pygame.display.set_mode(self.res, pygame.RESIZABLE)
pygame.display.set_caption('PyCalc (units)')
self.screen.fill(BACKGROUND)
self.font = pygame.font.Font(fn, size_button)
self.fontres = pygame.font.Font(fn, size_disp)
self.clock = pygame.time.Clock()
self.inp = ""
def events(self):
global a, b
for event in pygame.event.get():
if event.type == pygame.QUIT: self.running = False
if event.type == pygame.VIDEORESIZE:
self.res = event.w, event.h
self.last = 0
self.screen = pygame.display.set_mode(self.res, pygame.RESIZABLE)
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
boxx, boxy = self.res[0] // HSIZE, self.res[1] // VSIZE
X, Y = x // boxx, y // boxy
if Y == 0 or Y > VSIZE - 1 or X > HSIZE - 1:
return
ci = but[Y - 1][X]
if ci == "=": # evaluate expression
try:
inp2 = self.inp
for u1, u2 in us_units:
inp2 = inp2.replace(u1, u2)
self.inp = str(eval(inp2))
a = eval(self.inp)
except:
self.inp += " *ERROR*"
elif ci == "C": # clear screen
self.inp = ""
elif ci == "SCI": # convert to scientific notation
try:
self.inp = "%e" % float(self.inp)
except:
pass
else: # or add button text to input
self.inp += but[Y - 1][X]
def run(self):
self.running = True
while self.running:
self.clock.tick(10)
self.events()
self.update()
pygame.quit()
def update(self):
self.screen.fill(BACKGROUND)
boxx, boxy = self.res[0] // HSIZE, self.res[1] // VSIZE
for y in range(1, VSIZE):
for x in range(HSIZE):
ci = int(col_ind[y - 1][x])
pygame.draw.rect(self.screen, cols[ci],
[x * boxx + BORDER, y * boxy + BORDER,
boxx - 2 * BORDER, boxy - 2 * BORDER])
t = but[y - 1][x]
if t[-1] == "(" and t != "(":
t = t[:-1]
if t != "#":
tr = self.font.render(t, True, button_rgb)
ox = max(0, (boxx - tr.get_width()) // 2)
oy = max(0, (boxy - tr.get_height()) // 2)
self.screen.blit(tr, (x * boxx + BORDER + ox, y * boxy + BORDER + oy))
if len(self.inp) < MAXLEN: # check if maximum digits exceeded
t = self.inp
else:
try:
self.inp = "%e" % float(self.inp)
t = self.inp
except:
t = "*TOO LONG*"
tr = self.fontres.render(t, True, display_rgb)
self.screen.blit(tr, (0, 0))
pygame.display.flip()
app = PyCalc()
app.run()