L
Lo?c Mah?
Hello
I try to use a Validator for a TxtCtrl placed in a Panel with a Button
in order to trigger the Validator and test the content of TxtCtrl.
I have looked into wxPython documentation and demo and searched in
google, but I am still unsuccessful yet.
My problem is that I do not manage to call the Validate() method of my
Validator from the button handler.
When I try to run my program I always have the following error:
AttributeError: 'Frame' object has no attribute 'Validate'
This error occurs when I call the Validate method inside my OnSave
button handler.
I tried to call the Validate method on different parents objects of
the
button and text control without success.
Thanks for you help!
Loïc
Here is the code:
#!/usr/bin/env python
import wx
class TextObjectValidator(wx.PyValidator):
def __init__(self):
print "init"
wx.PyValidator.__init__(self)
def Clone(self):
print "Clone"
return TextObjectValidator()
def Validate(self, win):
print "Validate"
textCtrl = self.GetWindow()
text = textCtrl.GetValue()
if len(text) == 0:
wx.MessageBox("A text object must contain some text!",
"Error")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour(
wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
textCtrl.Refresh()
return True
def TransferToWindow(self):
print "TransferToWindow"
return True # Prevent wxDialog from complaining.
def TransferFromWindow(self):
print "TransferFromWindow"
return True # Prevent wxDialog from complaining.
class Frame(wx.Frame):
def __init__(self, parent=None, id=-1, title='<Title here>',
pos=wx.DefaultPosition, size=(400, 200)):
wx.Frame.__init__(self, parent, id, title, pos, size)
self.CenterOnScreen()
panel = wx.Panel(self, -1)
panel.SetBackgroundColour(wx.Colour(255, 255, 255))
fgs = wx.FlexGridSizer(cols=2, vgap=4, hgap=4)
self.label = wx.StaticText(panel, -1, 'word'+":")
self.tc = wx.TextCtrl(panel, -1, 'word', size=(50,-1),
validator=TextObjectValidator() )
fgs.Add(self.label, 1, flag=wx.ALIGN_RIGHT |
wx.ALIGN_CENTER_VERTICAL)
fgs.Add(self.tc, 1, flag=wx.EXPAND|wx.RIGHT, border=25)
b = wx.Button(panel, 2000, "Save" )
fgs.Add(b, 1, flag=wx.EXPAND | wx.RIGHT)
wx.EVT_BUTTON(panel, b.GetId(), self.OnSave)
panel.InitDialog()
panel.SetSizer( fgs )
panel.SetAutoLayout(1)
def OnSave(self, event):
print "OnSave"
### ERROR BELOW:
if self.Validate():
print "Validate OK"
else:
print "Validate KO"
pass
class App(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App()
app.MainLoop()
if __name__ == '__main__':
main()
I try to use a Validator for a TxtCtrl placed in a Panel with a Button
in order to trigger the Validator and test the content of TxtCtrl.
I have looked into wxPython documentation and demo and searched in
google, but I am still unsuccessful yet.
My problem is that I do not manage to call the Validate() method of my
Validator from the button handler.
When I try to run my program I always have the following error:
AttributeError: 'Frame' object has no attribute 'Validate'
This error occurs when I call the Validate method inside my OnSave
button handler.
I tried to call the Validate method on different parents objects of
the
button and text control without success.
Thanks for you help!
Loïc
Here is the code:
#!/usr/bin/env python
import wx
class TextObjectValidator(wx.PyValidator):
def __init__(self):
print "init"
wx.PyValidator.__init__(self)
def Clone(self):
print "Clone"
return TextObjectValidator()
def Validate(self, win):
print "Validate"
textCtrl = self.GetWindow()
text = textCtrl.GetValue()
if len(text) == 0:
wx.MessageBox("A text object must contain some text!",
"Error")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour(
wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
textCtrl.Refresh()
return True
def TransferToWindow(self):
print "TransferToWindow"
return True # Prevent wxDialog from complaining.
def TransferFromWindow(self):
print "TransferFromWindow"
return True # Prevent wxDialog from complaining.
class Frame(wx.Frame):
def __init__(self, parent=None, id=-1, title='<Title here>',
pos=wx.DefaultPosition, size=(400, 200)):
wx.Frame.__init__(self, parent, id, title, pos, size)
self.CenterOnScreen()
panel = wx.Panel(self, -1)
panel.SetBackgroundColour(wx.Colour(255, 255, 255))
fgs = wx.FlexGridSizer(cols=2, vgap=4, hgap=4)
self.label = wx.StaticText(panel, -1, 'word'+":")
self.tc = wx.TextCtrl(panel, -1, 'word', size=(50,-1),
validator=TextObjectValidator() )
fgs.Add(self.label, 1, flag=wx.ALIGN_RIGHT |
wx.ALIGN_CENTER_VERTICAL)
fgs.Add(self.tc, 1, flag=wx.EXPAND|wx.RIGHT, border=25)
b = wx.Button(panel, 2000, "Save" )
fgs.Add(b, 1, flag=wx.EXPAND | wx.RIGHT)
wx.EVT_BUTTON(panel, b.GetId(), self.OnSave)
panel.InitDialog()
panel.SetSizer( fgs )
panel.SetAutoLayout(1)
def OnSave(self, event):
print "OnSave"
### ERROR BELOW:
if self.Validate():
print "Validate OK"
else:
print "Validate KO"
pass
class App(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App()
app.MainLoop()
if __name__ == '__main__':
main()