Hi all!
I have a problem when i doing something at validator, i want to validate all widgets,
and when a widget validate false, it’s background color will be changed.
So, if all widgets validate false, all widget’s background color will be changed,
but the result is when first widget validate false, the rest of widgets don’t be validated.
Is the validator will stop when first widget get false? Or there is something wrong in my code?
Please help me, thanks!
Here is my code:
import wx
import pprint
class DataXferValidator(wx.PyValidator):
def init(self, data, key):
wx.PyValidator.init(self)
self.data = data
self.key = key
def Clone(self):
“”"
Note that every validator must implement the Clone() method.
“”"
return DataXferValidator(self.data, self.key)
def Validate(self, win):
widget = self.GetWindow()
text = widget.GetValue()
if len(text) == 0:
widget.SetBackgroundColour(’#ffcccc’)
widget.SetFocus()
widget.Refresh()
return False
else:
widget.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
widget.Refresh()
return True
def TransferToWindow(self):
textCtrl = self.GetWindow()
textCtrl.SetValue(self.data.get(self.key, “”))
return True
def TransferFromWindow(self):
textCtrl = self.GetWindow()
self.data[self.key] = textCtrl.GetValue()
return True
class MyDialog(wx.Dialog):
def init(self, data):
wx.Dialog.init(self, None, -1, “Validators: data transfer”)
Create the text controls
about = wx.StaticText(self, -1, about_txt)
name_l = wx.StaticText(self, -1, “Name:”)
email_l = wx.StaticText(self, -1, “Email:”)
phone_l = wx.StaticText(self, -1, “Phone:”)
name_t = wx.TextCtrl(self, validator=DataXferValidator(data, “name”))
email_t = wx.TextCtrl(self, validator=DataXferValidator(data, “email”))
phone_t = wx.TextCtrl(self, validator=DataXferValidator(data, “phone”))
Use standard button IDs
okay = wx.Button(self, wx.ID_OK, ‘OK’)
okay.SetDefault()
cancel = wx.Button(self, wx.ID_CANCEL, ‘Cancel’)
Layout with sizers
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(about, 0, wx.ALL, 5)
sizer.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5)
fgs = wx.FlexGridSizer(3, 2, 5, 5)
fgs.Add(name_l, 0, wx.ALIGN_RIGHT)
fgs.Add(name_t, 0, wx.EXPAND)
fgs.Add(email_l, 0, wx.ALIGN_RIGHT)
fgs.Add(email_t, 0, wx.EXPAND)
fgs.Add(phone_l, 0, wx.ALIGN_RIGHT)
fgs.Add(phone_t, 0, wx.EXPAND)
fgs.AddGrowableCol(1)
sizer.Add(fgs, 0, wx.EXPAND|wx.ALL, 5)
btns = wx.StdDialogButtonSizer()
btns.AddButton(okay)
btns.AddButton(cancel)
btns.Realize()
sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 5)
self.SetSizer(sizer)
sizer.Fit(self)
app = wx.PySimpleApp()
data = { “name” : “Lin zh",“email”:"lin@zh.com”}
dlg = MyDialog(data)
dlg.ShowModal()
dlg.Destroy()
wx.MessageBox(“You entered these values:\n\n” +
pprint.pformat(data))
app.MainLoop()