📄 numctrl.py
字号:
"""
if( self._min is None
or max is None
or (self._min is not None and self._min <= max) ):
try:
self.SetParameters(max=max)
bRet = True
except ValueError:
bRet = False
else:
bRet = False
return bRet
def GetMax(self):
"""
Gets the maximum value of the control. It will return the current
maximum integer, or None if not specified.
"""
return self._max
def SetBounds(self, min=None, max=None):
"""
This function is a convenience function for setting the min and max
values at the same time. The function only applies the maximum bound
if setting the minimum bound is successful, and returns True
only if both operations succeed.
NOTE: leaving out an argument will remove the corresponding bound.
"""
ret = self.SetMin(min)
return ret and self.SetMax(max)
def GetBounds(self):
"""
This function returns a two-tuple (min,max), indicating the
current bounds of the control. Each value can be None if
that bound is not set.
"""
return (self._min, self._max)
def SetLimited(self, limited):
"""
If called with a value of True, this function will cause the control
to limit the value to fall within the bounds currently specified.
If the control's value currently exceeds the bounds, it will then
be limited accordingly.
If called with a value of False, this function will disable value
limiting, but coloring of out-of-bounds values will still take
place if bounds have been set for the control.
"""
self.SetParameters(limited = limited)
def IsLimited(self):
"""
Returns True if the control is currently limiting the
value to fall within the current bounds.
"""
return self._limited
def GetLimited(self):
""" (For regularization of property accessors) """
return self.IsLimited
def IsInBounds(self, value=None):
"""
Returns True if no value is specified and the current value
of the control falls within the current bounds. This function can
also be called with a value to see if that value would fall within
the current bounds of the given control.
"""
## dbg('IsInBounds(%s)' % repr(value), indent=1)
if value is None:
value = self.GetValue()
else:
try:
value = self._GetNumValue(self._toGUI(value))
except ValueError, e:
## dbg('error getting NumValue(self._toGUI(value)):', e, indent=0)
return False
if value.strip() == '':
value = None
elif self._fractionWidth:
value = float(value)
else:
value = long(value)
min = self.GetMin()
max = self.GetMax()
if min is None: min = value
if max is None: max = value
# if bounds set, and value is None, return False
if value == None and (min is not None or max is not None):
## dbg('finished IsInBounds', indent=0)
return 0
else:
## dbg('finished IsInBounds', indent=0)
return min <= value <= max
def SetAllowNone(self, allow_none):
"""
Change the behavior of the validation code, allowing control
to have a value of None or not, as appropriate. If the value
of the control is currently None, and allow_none is False, the
value of the control will be set to the minimum value of the
control, or 0 if no lower bound is set.
"""
self._allowNone = allow_none
if not allow_none and self.GetValue() is None:
min = self.GetMin()
if min is not None: self.SetValue(min)
else: self.SetValue(0)
def IsNoneAllowed(self):
return self._allowNone
def GetAllowNone(self):
""" (For regularization of property accessors) """
return self.IsNoneAllowed()
def SetAllowNegative(self, value):
self.SetParameters(allowNegative=value)
def IsNegativeAllowed(self):
return self._allowNegative
def GetAllowNegative(self):
""" (For regularization of property accessors) """
return self.IsNegativeAllowed()
def SetGroupDigits(self, value):
self.SetParameters(groupDigits=value)
def IsGroupingAllowed(self):
return self._groupDigits
def GetGroupDigits(self):
""" (For regularization of property accessors) """
return self.IsGroupingAllowed()
def SetGroupChar(self, value):
self.SetParameters(groupChar=value)
def GetGroupChar(self):
return self._groupChar
def SetDecimalChar(self, value):
self.SetParameters(decimalChar=value)
def GetDecimalChar(self):
return self._decimalChar
def SetSelectOnEntry(self, value):
self.SetParameters(selectOnEntry=value)
def GetSelectOnEntry(self):
return self._selectOnEntry
def SetAutoSize(self, value):
self.SetParameters(autoSize=value)
def GetAutoSize(self):
return self._autoSize
# (Other parameter accessors are inherited from base class)
def _toGUI( self, value, apply_limits = True ):
"""
Conversion function used to set the value of the control; does
type and bounds checking and raises ValueError if argument is
not a valid value.
"""
## dbg('NumCtrl::_toGUI(%s)' % repr(value), indent=1)
if value is None and self.IsNoneAllowed():
## dbg(indent=0)
return self._template
elif type(value) in (types.StringType, types.UnicodeType):
value = self._GetNumValue(value)
## dbg('cleansed num value: "%s"' % value)
if value == "":
if self.IsNoneAllowed():
## dbg(indent=0)
return self._template
else:
## dbg('exception raised:', e, indent=0)
raise ValueError ('NumCtrl requires numeric value, passed %s'% repr(value) )
# else...
try:
if self._fractionWidth or value.find('.') != -1:
value = float(value)
else:
value = long(value)
except Exception, e:
## dbg('exception raised:', e, indent=0)
raise ValueError ('NumCtrl requires numeric value, passed %s'% repr(value) )
elif type(value) not in (types.IntType, types.LongType, types.FloatType):
## dbg(indent=0)
raise ValueError (
'NumCtrl requires numeric value, passed %s'% repr(value) )
if not self._allowNegative and value < 0:
raise ValueError (
'control configured to disallow negative values, passed %s'% repr(value) )
if self.IsLimited() and apply_limits:
min = self.GetMin()
max = self.GetMax()
if not min is None and value < min:
## dbg(indent=0)
raise ValueError (
'value %d is below minimum value of control'% value )
if not max is None and value > max:
## dbg(indent=0)
raise ValueError (
'value %d exceeds value of control'% value )
adjustwidth = len(self._mask) - (1 * self._useParens * self._signOk)
## dbg('len(%s):' % self._mask, len(self._mask))
## dbg('adjustwidth - groupSpace:', adjustwidth - self._groupSpace)
## dbg('adjustwidth:', adjustwidth)
if self._fractionWidth == 0:
s = str(long(value)).rjust(self._integerWidth)
else:
format = '%' + '%d.%df' % (self._integerWidth+self._fractionWidth+1, self._fractionWidth)
s = format % float(value)
## dbg('s:"%s"' % s, 'len(s):', len(s))
if len(s) > (adjustwidth - self._groupSpace):
## dbg(indent=0)
raise ValueError ('value %s exceeds the integer width of the control (%d)' % (s, self._integerWidth))
elif s[0] not in ('-', ' ') and self._allowNegative and len(s) == (adjustwidth - self._groupSpace):
## dbg(indent=0)
raise ValueError ('value %s exceeds the integer width of the control (%d)' % (s, self._integerWidth))
s = s.rjust(adjustwidth).replace('.', self._decimalChar)
if self._signOk and self._useParens:
if s.find('-') != -1:
s = s.replace('-', '(') + ')'
else:
s += ' '
## dbg('returned: "%s"' % s, indent=0)
return s
def _fromGUI( self, value ):
"""
Conversion function used in getting the value of the control.
"""
## dbg(suspend=0)
## dbg('NumCtrl::_fromGUI(%s)' % value, indent=1)
# One or more of the underlying text control implementations
# issue an intermediate EVT_TEXT when replacing the control's
# value, where the intermediate value is an empty string.
# So, to ensure consistency and to prevent spurious ValueErrors,
# we make the following test, and react accordingly:
#
if value.strip() == '':
if not self.IsNoneAllowed():
## dbg('empty value; not allowed,returning 0', indent = 0)
if self._fractionWidth:
return 0.0
else:
return 0
else:
## dbg('empty value; returning None', indent = 0)
return None
else:
value = self._GetNumValue(value)
## dbg('Num value: "%s"' % value)
if self._fractionWidth:
try:
## dbg(indent=0)
return float( value )
except ValueError:
## dbg("couldn't convert to float; returning None")
return None
else:
raise
else:
try:
## dbg(indent=0)
return int( value )
except ValueError:
try:
## dbg(indent=0)
return long( value )
except ValueError:
## dbg("couldn't convert to long; returning None")
return None
else:
raise
else:
## dbg('exception occurred; returning None')
return None
def _Paste( self, value=None, raise_on_invalid=False, just_return_value=False ):
"""
Preprocessor for base control paste; if value needs to be right-justified
to fit in control, do so prior to paste:
"""
## dbg('NumCtrl::_Paste (value = "%s")' % value, indent=1)
if value is None:
paste_text = self._getClipboardContents()
else:
paste_text = value
sel_start, sel_to = s
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -