473,501 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 7513
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2139
by: mdk.R | last post by:
Hello all: i'am installed wxPython 2.5 and Python2.3.4..i try execute script with wxPython but it show error: Traceback (most recent call last): File "E:\py\test.py", line 7, in ? import wx...
1
4051
by: Michael ALbanese | last post by:
I am developing a telephone directory for my company. I have Fist Name, Last Name, Department and Phone Number as fields. A user can search by entering data into any one, or combination of these...
2
380
by: Evgueni | last post by:
Hello, I am new to .NET, and a custom validator is giving me a lot of grief. I want to use a Custom Validation control and for some reasons it's not firing the validation procedure. I am using...
10
5429
by: Rigs | last post by:
Hi, I have a textbox with a Custom Validator that utilizes the OnServerValidate method for that textbox. This works fine, however the method only executes when data exists in that textbox after...
1
1928
by: Ben | last post by:
i'm having trouble getting a custom validator to fire on one of my webforms. i dragged a custom validator onto the form, left all the properties on default, double clicked it, and typed this in the...
9
5507
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
3
8231
by: Andy | last post by:
Hi folks, I have a customvalidator control that works properly if it isn't contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I find that _ServerValidate doesn't get fired,...
0
1882
by: OceanBreeze | last post by:
I have added a LinkButton to a table cell programmatically inside the Page_Load method. I also added a custom event to that link button. The same custom event is valid for all the link buttons. The...
9
4401
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
0
7164
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7046
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4958
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3127
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1463
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
330
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.