-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin.py
More file actions
62 lines (47 loc) · 1.97 KB
/
win.py
File metadata and controls
62 lines (47 loc) · 1.97 KB
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
from parser import UIParser
def clickClear(parser):
parser.getWidget('textField').config(text='')
def clickDigit(parser, digit):
label = parser.getWidget('textField')
label.config(text = label.cget('text') + str(digit))
def clickPoint(parser):
label = parser.getWidget('textField')
label = parser.getWidget('textField')
text = label.cget('text')
if len(text) > 0 and text[-1].isdigit():
label.config(text = label.cget('text') + '.')
def clickOperation(parser, operation):
operationSymbols = {'Mult': '*', 'Add': '+', 'Div': '/', 'Sub': '-', 'Equal': ''}
label = parser.getWidget('textField')
text = label.cget('text')
if (len(text) == 0): return None
if text[-1] in ['*', '/', '+', '-']: text = text[:-1:]
operations = {
'*': lambda a, b: a * b,
'/': lambda a, b: a / b,
'+': lambda a, b: a + b,
'-': lambda a, b: a - b
}
for sym in ['*', '/', '+', '-']:
if sym in text:
pos = text.find(sym)
text = '%.10f' % operations[text[pos]](float(text[:pos:]), float(text[pos+1::]))
break
while '.' in text and len(text) > 0 and (text[-1] == '0' or text[-1] == '.'):
text = text[:-1:]
if len(text) == 0:
text = '0'
label.config(text = text + operationSymbols[operation])
def clickOperationFactory(operation):
return lambda parser, operation=operation: clickOperation(parser, operation)
def clickDigitFactory(digit):
return lambda parser, digit=digit: clickDigit(parser, digit)
digitSignals = {'click'+str(digit): clickDigitFactory(digit) for digit in range(10)}
operationSignals = {'click'+operation: clickOperationFactory(operation) for operation in ['Mult', 'Add', 'Div', 'Sub', 'Equal']}
parser = UIParser('win.ui', {
**digitSignals,
**operationSignals,
'clickClear': clickClear,
'clickPoint': clickPoint,
})
parser.getWidget('mainWindow').mainloop()