468,115 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,115 developers. It's quick & easy.

[wxPython] Validator for TxtCtrl to trigger with a custom Button handler

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()
Jul 18 '05 #1
3 6968
def _onButton_(self, event):
success = self._myTextCtrl.Validate()
return

self is the Frame here. _onButton_ therefore belongs to the frame and was
bound with wx.Button.Bind.

HTH
F. GEIGER
"Lo?c Mah?" <lo*******@free.fr> wrote in message
news:27**************************@posting.google.c om...
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()

Jul 18 '05 #2
Your solution does not seem work, at least on my system!
Does it works on your system ?

I got a message telling me that:

_myTextCtrl / self.tc has no Validate attribute
I have:
windows
python 2.3.4
wxpython 2.5.1
I finnaly got a kind of ugly workaround to call the Validate() function:

I had to replace:
if self.Validate():

by:
if self.tc.GetValidator().Validate(self.tc):

I find this quite ugly and not pythoninc at all,
but this is the only solution I found yet!

Loic
"F. GEIGER" <fr**********@fh-vorarlberg.ac.at> wrote in message news:<40***********************@aconews.univie.ac. at>...
def _onButton_(self, event):
success = self._myTextCtrl.Validate()
return

self is the Frame here. _onButton_ therefore belongs to the frame and was
bound with wx.Button.Bind.

HTH
F. GEIGER

Jul 18 '05 #3
Sorry, must read:

def _onButton_(self, event):
success = self._myTextCtrl.GetValidator().Validate()
return

CAUTION: This code has never executed!

Of course, you have to supply _myTextCtrl with a validator upon creation.

Look at the wxPython demo: Core Windows Controls -> Validator. Have a look
into the help files, look for wxValidator.

HTH
Franz GEIGER

"Lo?c Mah?" <lo*******@free.fr> wrote in message
news:27**************************@posting.google.c om...
Your solution does not seem work, at least on my system!
Does it works on your system ?

I got a message telling me that:

_myTextCtrl / self.tc has no Validate attribute
I have:
windows
python 2.3.4
wxpython 2.5.1
I finnaly got a kind of ugly workaround to call the Validate() function:

I had to replace:
if self.Validate():

by:
if self.tc.GetValidator().Validate(self.tc):

I find this quite ugly and not pythoninc at all,
but this is the only solution I found yet!

Loic
"F. GEIGER" <fr**********@fh-vorarlberg.ac.at> wrote in message

news:<40***********************@aconews.univie.ac. at>...
def _onButton_(self, event):
success = self._myTextCtrl.Validate()
return

self is the Frame here. _onButton_ therefore belongs to the frame and was bound with wx.Button.Bind.

HTH
F. GEIGER

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by mdk.R | last post: by
1 post views Thread by Michael ALbanese | last post: by
2 posts views Thread by Evgueni | last post: by
1 post views Thread by Ben | last post: by
3 posts views Thread by Andy | last post: by
5 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.