-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNumberControl.py
211 lines (174 loc) · 6.28 KB
/
NumberControl.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
205
206
207
208
209
210
211
import math
from sigfig import *
# import wx
# #Overwrite the TextCtrl Class in order to control the float input
# class MyTextCtrl(wx.TextCtrl):
# def __init__(self, *args, **kwargs):
# super(MyTextCtrl, self).__init__(*args, **kwargs)
# self.preValue = ""
#allow only the float number type inputs
def FloatNumberControl(evt):
ctrl = evt.GetEventObject()
value = ctrl.GetValue().strip()
insertPoint = ctrl.GetInsertionPoint()
digits = len(value) - len(ctrl.preValue)
try:
float(value)
ctrl.preValue = value
ctrl.ChangeValue(value)
# ctrl.SetInsertionPoint(insertPoint + digits)
except:
if ctrl.GetValue() == '':
ctrl.preValue = ''
# ctrl.SetInsertionPoint(insertPoint + digits)
elif ctrl.GetValue() == '.':
ctrl.SetValue('0.')
ctrl.preValue = '.'
ctrl.SetInsertionPoint(-1)
# ctrl.SetInsertionPoint(insertPoint + digits)
elif ctrl.GetValue() == '-':
ctrl.preValue = '-'
# ctrl.SetInsertionPoint(insertPoint + digits)
elif ctrl.GetValue() == '-.':
ctrl.SetValue('-0.')
ctrl.preValue = '-.'
ctrl.SetInsertionPoint(-1)
# ctrl.SetInsertionPoint(insertPoint + digits)
elif ctrl.GetValue() == '+':
ctrl.preValue = '+'
# ctrl.SetInsertionPoint(insertPoint + digits)
elif ctrl.GetValue() == '+.':
ctrl.SetValue('+0.')
ctrl.preValue = '+.'
ctrl.SetInsertionPoint(-1)
# ctrl.SetInsertionPoint(insertPoint + digits)
else:
# insertPoint = ctrl.GetInsertionPoint() - digits
ctrl.SetValue(ctrl.preValue)
ctrl.SetInsertionPoint(insertPoint - digits)
# print "go back"
evt.Skip()
#Rounding by significant number
def RoundSigfigs(num, sig_figs):
if num == "":
return ""
try:
num = float(num)
except:
return 0
if num != 0:
# result = round(num, -int(math.floor(math.log10(abs(num))) - (sig_figs - 1)))
result = round_sig(num, sig_figs)
return result
# if result == int(result):
# return int(result)
# else:
# return result
else:
return 0 # Can't take the log of 0
#rounding by digit
def Round(digit, ctrl):
if ctrl.GetValue() == "":
return
if ctrl.GetValue() == "+" or ctrl.GetValue() == "-" or ctrl.GetValue() == "." \
or ctrl.GetValue() == "+." or ctrl.GetValue() == "-.":
ctrl.ChangeValue("")
ctrl.preValue = ""
return
strDigit = '{0:.' + str(digit) + 'f}'
val = strDigit.format(float(ctrl.GetValue()) + 10**(-10))
# print val
ctrl.ChangeValue(val)
ctrl.preValue = val
def RoundByCtrl1(ctrl):
insertPoint = ctrl.GetInsertionPoint()
Round(1, ctrl)
ctrl.SetInsertionPoint(insertPoint)
def RoundByCtrl2(ctrl):
insertPoint = ctrl.GetInsertionPoint()
Round(2, ctrl)
ctrl.SetInsertionPoint(insertPoint)
def RoundByCtrl3(ctrl):
insertPoint = ctrl.GetInsertionPoint()
Round(3, ctrl)
ctrl.SetInsertionPoint(insertPoint)
def RoundByCtrl4(ctrl):
insertPoint = ctrl.GetInsertionPoint()
Round(4, ctrl)
ctrl.SetInsertionPoint(insertPoint)
def RoundByCtrl5(ctrl):
insertPoint = ctrl.GetInsertionPoint()
Round(5, ctrl)
ctrl.SetInsertionPoint(insertPoint)
#round to 1 decimal points
def Round1(event):
insertPoint = event.GetEventObject().GetInsertionPoint()
Round(1, event.GetEventObject())
event.GetEventObject().SetInsertionPoint(insertPoint)
event.Skip()
#round to 2 decimal points
def Round2(event):
insertPoint = event.GetEventObject().GetInsertionPoint()
Round(2, event.GetEventObject())
event.GetEventObject().SetInsertionPoint(insertPoint)
event.Skip()
#round to 3 decimal points
def Round3(event):
insertPoint = event.GetEventObject().GetInsertionPoint()
Round(3, event.GetEventObject())
event.GetEventObject().SetInsertionPoint(insertPoint)
event.Skip()
#round to 4 decimal points
def Round4(event):
insertPoint = event.GetEventObject().GetInsertionPoint()
Round(4, event.GetEventObject())
event.GetEventObject().SetInsertionPoint(insertPoint)
event.Skip()
#round to 5 decimal points
def Round5(event):
insertPoint = event.GetEventObject().GetInsertionPoint()
Round(5, event.GetEventObject())
event.GetEventObject().SetInsertionPoint(insertPoint)
event.Skip()
#Keep 1 significant digit
def Sig1(event):
event.GetEventObject().ChangeValue(str(RoundSigfigs(event.GetEventObject().GetValue(), 1)))
event.GetEventObject().preValue = str(RoundSigfigs(event.GetEventObject().GetValue(), 1))
event.Skip()
#Keep 2 significant digit
def Sig2(event):
event.GetEventObject().ChangeValue(str(RoundSigfigs(event.GetEventObject().GetValue(), 2)))
event.GetEventObject().preValue = str(RoundSigfigs(event.GetEventObject().GetValue(), 2))
event.Skip()
#Keep 3 significant digit
def Sig3(event):
event.GetEventObject().ChangeValue(str(RoundSigfigs(event.GetEventObject().GetValue(), 3)))
event.GetEventObject().preValue = str(RoundSigfigs(event.GetEventObject().GetValue(), 3))
event.Skip()
#Keep 4 significant digit
def Sig4(event):
event.GetEventObject().ChangeValue(str(RoundSigfigs(event.GetEventObject().GetValue(), 4)))
event.GetEventObject().preValue = str(RoundSigfigs(event.GetEventObject().GetValue(), 4))
event.Skip()
def SigByCtrl1(ctrl):
ctrl.ChangeValue(str(RoundSigfigs(ctrl.GetValue(), 1)))
ctrl.preValue = str(RoundSigfigs(ctrl.GetValue(), 1))
def SigByCtrl2(ctrl):
ctrl.ChangeValue(str(RoundSigfigs(ctrl.GetValue(), 2)))
ctrl.preValue = str(RoundSigfigs(ctrl.GetValue(), 2))
def SigByCtrl3(ctrl):
ctrl.ChangeValue(str(RoundSigfigs(ctrl.GetValue(), 3)))
ctrl.preValue = str(RoundSigfigs(ctrl.GetValue(), 3))
def SigByCtrl4(ctrl):
ctrl.ChangeValue(str(RoundSigfigs(ctrl.GetValue(), 4)))
ctrl.preValue = str(RoundSigfigs(ctrl.GetValue(), 4))
#Allow only integer
def OnChar_int(event):
key = event.GetKeyCode()
acceptable_characters = "1234567890."
if chr(key) in acceptable_characters:
event.Skip()
return
else:
event.Skip()
return False