Hello,
I'm a newbie, but I've succeeded in getting a decent application up and running (http://stv.sourceforge.net), thanks to the extensive examples that come with wxPython. I have a question about validating user input.
In my app I ask the user for a float. To validate, I override the default OK button processing in the dialog. Here is an example for making sure that the user entered a float:
def OnOK(self, event):
if re.search("^\s*(\d+|\d*\.\d+|\d+\.\d*)\s*$", self.Cutoff):
event.Skip()
else:
dlg = wx.MessageDialog(self.Cutoff must be a float.",
"", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
This seems easier than using other validation methods. I tried the masked text controls, but I don't like how the control is initialized with spaces.
Any comments on the above?
Jeff
What about simply converting the value entered, like:
def OnOK(self, event):
try:
val = float(self.Cutoff)
event.Skip()
except: # or except ValueError:
dlg = ...
...
Note, float() ignores leading and trailing white spaces.
/Jean Brouwers
Jeffrey O'Neill wrote:
···
Hello,
I'm a newbie, but I've succeeded in getting a decent application up and running (http://stv.sourceforge.net), thanks to the extensive examples that come with wxPython. I have a question about validating user input.
In my app I ask the user for a float. To validate, I override the default OK button processing in the dialog. Here is an example for making sure that the user entered a float:
def OnOK(self, event):
if re.search("^\s*(\d+|\d*\.\d+|\d+\.\d*)\s*$", self.Cutoff):
event.Skip()
else:
dlg = wx.MessageDialog(self.Cutoff must be a float.",
"", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
This seems easier than using other validation methods. I tried the masked text controls, but I don't like how the control is initialized with spaces.
Any comments on the above?
Jeff
The easiest (and most reliable) I know is:
try:
val = float(inputstring)
except ValueError:
# inputstring is not a valid float literal
HandleTheError
-Chris
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
···
On Thursday, January 8, 2004, at 11:34 AM, Jeffrey O'Neill wrote:
In my app I ask the user for a float. To validate, I override the default OK button processing in the dialog. Here is an example for making sure that the user entered a float:
def OnOK(self, event):
if re.search("^\s*(\d+|\d*\.\d+|\d+\.\d*)\s*$", self.Cutoff):
event.Skip()
else:
dlg = wx.MessageDialog(self.Cutoff must be a float.",
"", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()