473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

[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 7499
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.