-
Notifications
You must be signed in to change notification settings - Fork 11
/
floatvalidator.py
executable file
·67 lines (51 loc) · 1.55 KB
/
floatvalidator.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
import wx
import string
import mtexts
class FloatValidator(wx.PyValidator):
def __init__(self, minim=None, maxim=None):
wx.PyValidator.__init__(self)
self.minim = minim
self.maxim = maxim
self.Bind(wx.EVT_CHAR, self.OnChar)
def Clone(self):
return FloatValidator(self.minim, self.maxim)
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
def Validate(self, win):
tc = self.GetWindow()
val = tc.GetValue()
if (val == ''):
dlgm = wx.MessageDialog(None, mtexts.txts['FieldsCannotBeEmpty'], mtexts.txts['Error'], wx.OK|wx.ICON_EXCLAMATION)
dlgm.ShowModal()
dlgm.Destroy()
return False
if ((self.minim != None and float(val) < self.minim) or (self.maxim != None and float(val) >= self.maxim)):
s = mtexts.txts['RangeError2']
if (self.maxim != None):
s = mtexts.txts['RangeError3'] + '%2.1f' % self.maxim
dlgm = wx.MessageDialog(None, s, mtexts.txts['Error'], wx.OK|wx.ICON_EXCLAMATION)
dlgm.ShowModal()
dlgm.Destroy()
return False
return True
def OnChar(self, event):
# FIXME: something is wrong with this code, KeyCode
# behavior has been changed in wxPython 2.8.1?
#print event, type(event), event.KeyCode
key = event.KeyCode
if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
event.Skip()
return
tc = self.GetWindow()
val = tc.GetValue()
dot = False
for x in val:
if x == '.':
dot = True
if (chr(key) in string.digits or ((not dot) and chr(key) == '.')):
event.Skip()
return
if not wx.Validator_IsSilent():
wx.Bell()