-
Notifications
You must be signed in to change notification settings - Fork 11
/
intvalidator.py
executable file
·57 lines (42 loc) · 1.3 KB
/
intvalidator.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
import wx
import string
import mtexts
class IntValidator(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 IntValidator(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['NumFieldsCannotBeEmpty'], mtexts.txts['Error'], wx.OK|wx.ICON_EXCLAMATION)
dlgm.ShowModal()
dlgm.Destroy()
return False
if ((self.minim != None and int(val) < self.minim) or (self.maxim != None and int(val) > self.maxim)):
dlgm = wx.MessageDialog(None, mtexts.txts['RangeError'], 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
if chr(key) in string.digits:
event.Skip()
return
if not wx.Validator_IsSilent():
wx.Bell()