A simple question I can't seem to find an answer for:
I've got a series of wx.TextCtrl objects on the panel which hold
numeric values. Upon any of them being edited I respond to the
wx.EVT_TEXT event, grab the values from the controls and recalculate
some things. My question is how best to validate that the input in
each control is a number and not a character (typo etc..). At first, I
thought I would do this:
if (isinstance(self.spanTextCtrl.GetValue(),(int,long,float,complex))):
span = int(self.spanTextCtrl.GetValue())
But I realize that GetValue() returns a 'unicode' type object no
matter what's in the box, and that trying to int() that value will
halt the program.
How is this properly handled? I'm assuming it's something that has to
be done in a wide range of programs, yet I can't find any standard way
of doing it. Is there a property or method of the wx.TextCtrl object
that will let me mask the input prior to even getting to my validation
function?
A simple question I can't seem to find an answer for:
I've got a series of wx.TextCtrl objects on the panel which hold
numeric values. Upon any of them being edited I respond to the
wx.EVT_TEXT event, grab the values from the controls and recalculate
some things. My question is how best to validate that the input in
each control is a number and not a character (typo etc..). At first, I
thought I would do this:
if (isinstance(self.spanTextCtrl.GetValue(),(int,long,float,complex))):
span = int(self.spanTextCtrl.GetValue())
But I realize that GetValue() returns a 'unicode' type object no
matter what's in the box, and that trying to int() that value will
halt the program.
How is this properly handled? I'm assuming it's something that has to
be done in a wide range of programs, yet I can't find any standard way
of doing it. Is there a property or method of the wx.TextCtrl object
that will let me mask the input prior to even getting to my validation
function?
It sounds like what you want is an IntCtrl, which you can find an example of in the demo. It allows you to set a minimum and maximum optionally, and it won't allow non-numeric characters to be typed (except for a negative sign if the minimum is below zero).
There are more complex ways to do custom validation but if all you want are integers no need to reinvent the wheel.
I guess then you'd have to use a Try..Except to catch the ValueError
and deal with it?
I've implemented the NumCtrl but I'm not crazy about the way it tabs
from whole to fractional parts of the field and the font in the
NumCtrl fields is different than the standard font. Any way to change
that?
···
On 4/24/07, Christopher Barker <Chris.Barker@noaa.gov> wrote:
goo idea, but for the record, calling int() [or float()] on a Unicode
string is a fine way to check if it's valid:
I guess then you'd have to use a Try..Except to catch the ValueError
and deal with it?
exactly.
-CHB
···
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
I guess then you'd have to use a Try..Except to catch the ValueError
and deal with it?
It's a simple idiom. You can play around with this:
while(True):
data = raw_input("Enter an int: ")
try:
data = int(data)
break #if the conversion doesn't work, this doesn't execute
except ValueError:
pass
Note: the conversion won't work for decimal numbers either--if there is
a character in the string that is not a digit, int() throws an exceptions, and
therefore a decimal point causes an exception.