]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/val.py
minor clean up
[wxWidgets.git] / wxPython / tests / val.py
1 from wxPython.wx import *
2 import string
3
4
5 class floatValidator(wxPyValidator):
6
7 def __init__(self, obj=None, attrName=""):
8 wxPyValidator.__init__(self)
9 self.numList = ['1','2','3','4','5','6','7','8','9','0','.']
10 EVT_CHAR(self, self.OnChar)
11 self.obj = obj
12 self.attrName = attrName
13
14 def Clone(self):
15 return floatValidator(self.obj, self.attrName)
16
17 def TransferToWindow(self):
18 if self.obj and hasattr(self.obj, self.attrName):
19 tc = wxPyTypeCast(self.GetWindow(), "wxTextCtrl")
20 tc.SetValue(str(getattr(self.obj, self.attrName)))
21 return true
22
23 def TransferFromWindow(self):
24 if self.obj and self.attrName:
25 tc = wxPyTypeCast(self.GetWindow(), "wxTextCtrl")
26 text = tc.GetValue()
27 setattr(self.obj, self.attrName, string.atof(text))
28 return true
29
30
31 def Validate(self, win):
32 tc = wxPyTypeCast(self.GetWindow(), "wxTextCtrl")
33 val = tc.GetValue()
34
35 for x in val:
36 if x not in self.numList:
37 return false
38
39 return true
40
41 def OnChar(self, event):
42 key = event.KeyCode()
43 if key < WXK_SPACE or key == WXK_DELETE or key > 255:
44 event.Skip()
45 return
46
47 if chr(key) in self.numList:
48 event.Skip()
49 return
50
51 if not wxValidator_IsSilent():
52 wxBell()
53
54 # Returning without calling even.Skip eats the event before it
55 # gets to the text control
56 return
57
58
59
60 class MyDialog(wxDialog):
61 def __init__(self, parent):
62 wxDialog.__init__(self, parent, -1, "hello")
63
64 self.theValue = 555.12
65 fltValidator = floatValidator(self, "theValue")
66
67 Vbox = wxBoxSizer(wxVERTICAL)
68 Tbox = wxBoxSizer(wxHORIZONTAL)
69 Tbox.Add(wxStaticText(self, -1, "Initial Balance"), 0, wxALL,5)
70 Tbox.Add(wxTextCtrl(self, 13, "123.45", validator = fltValidator,
71 size=(100, -1)), 0, wxALL,5)
72
73 Vbox.Add(Tbox, 0, 0)
74
75 Tbox = wxBoxSizer(wxHORIZONTAL)
76 Tbox.Add(wxButton(self, wxID_OK, "Ok"), 0, wxALL,5)
77 Tbox.Add(wxButton(self, wxID_CANCEL, "Cancel"), 0, wxALL,5)
78
79 Vbox.Add(Tbox, 0, 0)
80
81 self.SetAutoLayout(true)
82 self.SetSizer(Vbox)
83 Vbox.Fit(self)
84
85
86 class TestFrame(wxFrame):
87 def __init__(self, parent):
88 wxFrame.__init__(self, parent, -1, "Testing...", size=(150,75))
89 wxButton(self, 25, "Click Me")
90 EVT_BUTTON(self, 25, self.OnClick)
91
92
93 def OnClick(self, event):
94 dlg = MyDialog(self)
95 dlg.ShowModal()
96 print dlg.theValue
97 dlg.Destroy()
98
99
100
101 app = wxPySimpleApp()
102 frame = TestFrame(None)
103 frame.Show(true)
104 app.MainLoop()
105
106
107
108