-
Notifications
You must be signed in to change notification settings - Fork 14
/
calculator_trinket.py
109 lines (91 loc) · 3.71 KB
/
calculator_trinket.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
'''
A simple Python calculator gui application based loosely on the Texas Instruments DataMath II
produced circa 1975
Author: Israel Dryer
Email: [email protected]
Modified: 2019-10-07
'''
import PySimpleGUI as sg
# default settings
bw: dict = {'size':(7,2), 'font':('Franklin Gothic Book', 24), 'button_color':("black","#F8F8F8")}
bt: dict = {'size':(7,2), 'font':('Franklin Gothic Book', 24), 'button_color':("black","#F1EABC")}
bo: dict = {'size':(16,2), 'font':('Franklin Gothic Book', 24), 'button_color':("black","#ECA527"), 'focus':True}
layout: list = [
[sg.Text('PyDataMath-II', size=(50,1), justification='right', background_color="#272533",
text_color='white', font=('Franklin Gothic Book', 14, 'bold'))],
[sg.Text('0.0000', size=(16,1), justification='right', background_color='black', text_color='red',
font=('Digital-7',51), relief='sunken', key="_DISPLAY_")],
[sg.Button('C',**bt), sg.Button('CE',**bt), sg.Button('%',**bt), sg.Button("/",**bt)],
[sg.Button('7',**bw), sg.Button('8',**bw), sg.Button('9',**bw), sg.Button("*",**bt)],
[sg.Button('4',**bw), sg.Button('5',**bw), sg.Button('6',**bw), sg.Button("-",**bt)],
[sg.Button('1',**bw), sg.Button('2',**bw), sg.Button('3',**bw), sg.Button("+",**bt)],
[sg.Button('0',**bw), sg.Button('.',**bw), sg.Button('=',**bo, bind_return_key=True)],
]
window: object = sg.Window('PyDataMath-II', layout=layout, element_justification='right', margins=(10,20), background_color="#272533", return_keyboard_events=True)
##----CALCULATOR FUNCTIONS-------------------------------##
var: dict = {'front':[], 'back':[], 'decimal':False, 'x_val':0.0, 'y_val':0.0, 'result':0.0, 'operator':''}
#-----HELPER FUNCTIONS
def format_number() -> float:
''' Create a consolidated string of numbers from front and back lists '''
return float(''.join(var['front']) + '.' + ''.join(var['back']))
def update_display(display_value: str):
''' Update the calculator display after an event click '''
try:
window['_DISPLAY_'].update(value='{:,.4f}'.format(display_value))
except:
window['_DISPLAY_'].update(value=display_value)
#-----CLICK EVENTS
def number_click(event: str):
''' Number button button click event '''
global var
if var['decimal']:
var['back'].append(event)
else:
var['front'].append(event)
update_display(format_number())
def clear_click():
''' CE or C button click event '''
global var
var['front'].clear()
var['back'].clear()
var['decimal'] = False
def operator_click(event: str):
''' + - / * button click event '''
global var
var['operator'] = event
try:
var['x_val'] = format_number()
except:
var['x_val'] = var['result']
clear_click()
def calculate_click():
''' Equals button click event '''
global var
var['y_val'] = format_number()
try:
var['result'] = eval(str(var['x_val']) + var['operator'] + str(var['y_val']))
update_display(var['result'])
clear_click()
except:
update_display("ERROR! DIV/0")
clear_click()
##-----MAIN EVENT LOOP-----------------------------------##
while True:
event, values = window.read()
print(event)
if event is None:
break
if event in ['0','1','2','3','4','5','6','7','8','9']:
number_click(event)
if event in ['Escape:27','C','CE']: # 'Escape:27 for keyboard control
clear_click()
update_display(0.0)
var['result'] = 0.0
if event in ['+','-','*','/']:
operator_click(event)
if event == '=':
calculate_click()
if event == '.':
var['decimal'] = True
if event == '%':
update_display(var['result'] / 100.0)