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

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()
 
F

F. GEIGER

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
 
L

Lo?c Mah?

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

F. GEIGER

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top