-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberPad.py
204 lines (168 loc) · 6.58 KB
/
NumberPad.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# provide a number pad to accept number input
# with calculator functions
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
import functools
import re
class NumberPad(Popup):
_font_size=12
_err_msgs=["EVAL ERROR","OUT OF RANGE"]
def __init__(self,init_value='',size=None, range=(None,None),**kwargs):
super(NumberPad,self).__init__(**kwargs)
if size is None:
self.size_hint = (1,1)
else:
self.size_hint = (None, None)
if size is None:
self.size=(200,200)
self._font_size = 16
else:
self.size=tuple(size)
self._font_size = self.size[0]/15
if range is None:
self.range = (None,None)
else:
self.range = range
self._vbox = BoxLayout(orientation='vertical')
self._vbox.padding=(8,8,8,8)
self._hbox1=BoxLayout(orientation='horizontal',size_hint_y=0.2)
# self._labValue=Label(text='Value',size_hint_x=0.2)
self._hbox11 = BoxLayout(orientation='horizontal')
self._inpValue=TextInput(readonly=True,multiline=False,size_hint_x=0.8,font_size=self._font_size)
self._btnClose = Button(text=chr(0x274C),size_hint_x=0.1,font_size=self._font_size)
self._hbox11.add_widget(self._inpValue)
self._hbox11.add_widget(self._btnClose)
# self._hbox1.add_widget(self._labValue)
self._hbox1.add_widget(self._hbox11)
self._vbox.add_widget(self._hbox1)
btns=[ ['7','8','9','+','DEL'],
['4','5','6','-','AC'],
['1','2','3','*','='],
['0','e','.','/','ENT']]
self._grid=GridLayout(rows=4,cols=5)
self._btns={}
for one_row in btns:
for b in one_row:
self._btns[b]=Button(text=b,font_size=self._font_size)
self._grid.add_widget(self._btns[b])
if b in ['0','1','2','3','4','5','6','7','8','9','0','e','.','+','-','*','/']:
self._btns[b].bind(on_release=functools.partial(self.set_value,b))
elif b=='DEL':
self._btns[b].bind(on_release=self.delete)
elif b=='AC':
self._btns[b].bind(on_release=self.clear)
elif b=='=':
self._btns[b].bind(on_release=self.equal)
elif b=='ENT':
self._btns[b].bind(on_release=self.accept)
self._vbox.add_widget(self._grid)
Window.bind(on_key_down=self.key_action)
self.content=self._vbox
self.auto_dismiss = False
self.title="Enter a number"
self._response = None
self._value1 = None
self._value2 = None
self._operation = None
self._last = None
self._callback_close = None
self._callback_escape = None
self._caller = None
if init_value:
self._inpValue.text = init_value
self._init_value=init_value
# check validity of the input
self._regex=re.compile('^[-+]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][-+]?[0-9]?)?$')
# title_label = self.children[0].children[2]
# title_label.height = 0
self.separator_height = 0
def key_action(self,*args):
keys={41:'ESC',42:'DEL',84:'/',85:'*',86:'-',87:'+',88:'RETURN',89:'1',90:'2',91:'3',92:'4',93:'5',94:'6',95:'7',96:'8',97:'9',98:'0',99:'.'}
# print('got a key',*args)
keyboard=args[0]
keycode=args[1]
text=args[2]
# print(keyboard, keycode, text,type(text))
if text in keys:
b=keys[text]
else:
return
if b in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e','.','+','-','*','/']:
self.set_value(b)
elif b=='RETURN':
self.equal()
elif b=='DEL':
self.delete()
elif b=='ESC':
self.dismiss()
self._callback_escape()
# self.dismiss()
def set_callback(self,foo_close,foo_escape,caller):
self._callback_close=foo_close
self._callback_escape=foo_escape
self._caller = caller
# self._btnClose.bind(on_press=lambda *args: self.dismiss())
self._btnClose.bind(on_press=self.on_close)
def on_close(self,*args):
self.dismiss()
self._caller._keyboard_escape()
def accept(self,*kwargs):
self.equal()
if self._regex.match(self._inpValue.text):
bRange = True
if self.range[0] is not None and self.range[0] > float(self._inpValue.text):
bRange = False
if self.range[1] is not None and self.range[1] < float(self._inpValue.text):
bRange = False
if bRange:
self.result=self._inpValue.text
self.dismiss()
self._callback_close()
else:
self._inpValue.text = self._err_msgs[1]
def set_value(self,b,*kwargs):
if self._inpValue.text in self._err_msgs:
self._inpValue.text = ""
self._inpValue.text += b
def delete(self,*kwargs):
if self._inpValue.text in self._err_msgs:
self._inpValue.text = ""
return
if not self._regex.match(self._inpValue.text):
self._inpValue.text=''
else:
tmp=len(self._inpValue.text)
self._inpValue.text = self._inpValue.text[:tmp-1]
pass
def clear(self,*kwargs):
self._inpValue.text=''
pass
def equal(self,*kwargs):
calc_entry = self._inpValue.text
try:
ans = str(eval(calc_entry))
self._inpValue.text = ans
except Exception as error:
self._inpValue.text = "EVAL ERROR"
pass
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
'''
Act when a key is pressed down
:param keyboard: The keyboard object
:param keycode: The pressed key code
:param text: The text that was pressed
:param modifiers: A list of keyboard modifiers
:return: True to accept the key, or False to ignore it
'''
if keycode[1] == 'escape':
# Dismiss popup upon 'Esc'
self.dismiss()
# Call the cancel_callback if we have it
if self.cancel_callback is not None:
self.cancel_callback()
# Grag all keyboard events so that the base app doesn't get anything
return True