📄 intctrl.py
字号:
## print 'disallowed'
## print
if allow_event:
if set_to_none:
wx.CallAfter(ctrl.SetValue, new_value)
elif set_to_zero:
# select to "empty" numeric value
wx.CallAfter(ctrl.SetValue, new_value)
wx.CallAfter(ctrl.SetInsertionPoint, 0)
wx.CallAfter(ctrl.SetSelection, 0, 1)
elif set_to_minus_one:
wx.CallAfter(ctrl.SetValue, new_value)
wx.CallAfter(ctrl.SetInsertionPoint, 1)
wx.CallAfter(ctrl.SetSelection, 1, 2)
elif not internally_set:
event.Skip() # allow base wxTextCtrl to finish processing
elif not wx.Validator_IsSilent():
wx.Bell()
def TransferToWindow(self):
""" Transfer data from validator to window.
The default implementation returns False, indicating that an error
occurred. We simply return True, as we don't do any data transfer.
"""
return True # Prevent wx.Dialog from complaining.
def TransferFromWindow(self):
""" Transfer data from window to validator.
The default implementation returns False, indicating that an error
occurred. We simply return True, as we don't do any data transfer.
"""
return True # Prevent wx.Dialog from complaining.
#----------------------------------------------------------------------------
class IntCtrl(wx.TextCtrl):
"""
This class provides a control that takes and returns integers as
value, and provides bounds support and optional value limiting.
IntCtrl(
parent, id = -1,
value = 0,
pos = wxDefaultPosition,
size = wxDefaultSize,
style = 0,
validator = wxDefaultValidator,
name = "integer",
min = None,
max = None,
limited = False,
allow_none = False,
allow_long = False,
default_color = wxBLACK,
oob_color = wxRED )
value
If no initial value is set, the default will be zero, or
the minimum value, if specified. If an illegal string is specified,
a ValueError will result. (You can always later set the initial
value with SetValue() after instantiation of the control.)
min
The minimum value that the control should allow. This can be
adjusted with SetMin(). If the control is not limited, any value
below this bound will be colored with the current out-of-bounds color.
If min < -sys.maxint-1 and the control is configured to not allow long
values, the minimum bound will still be set to the long value, but
the implicit bound will be -sys.maxint-1.
max
The maximum value that the control should allow. This can be
adjusted with SetMax(). If the control is not limited, any value
above this bound will be colored with the current out-of-bounds color.
if max > sys.maxint and the control is configured to not allow long
values, the maximum bound will still be set to the long value, but
the implicit bound will be sys.maxint.
limited
Boolean indicating whether the control prevents values from
exceeding the currently set minimum and maximum values (bounds).
If False and bounds are set, out-of-bounds values will
be colored with the current out-of-bounds color.
allow_none
Boolean indicating whether or not the control is allowed to be
empty, representing a value of None for the control.
allow_long
Boolean indicating whether or not the control is allowed to hold
and return a long as well as an int.
default_color
Color value used for in-bounds values of the control.
oob_color
Color value used for out-of-bounds values of the control
when the bounds are set but the control is not limited.
validator
Normally None, IntCtrl uses its own validator to do value
validation and input control. However, a validator derived
from IntValidator can be supplied to override the data
transfer methods for the IntValidator class.
"""
def __init__ (
self, parent, id=-1, value = 0,
pos = wx.DefaultPosition, size = wx.DefaultSize,
style = 0, validator = wx.DefaultValidator,
name = "integer",
min=None, max=None,
limited = 0, allow_none = 0, allow_long = 0,
default_color = wx.BLACK, oob_color = wx.RED,
):
# Establish attrs required for any operation on value:
self.__min = None
self.__max = None
self.__limited = 0
self.__default_color = wx.BLACK
self.__oob_color = wx.RED
self.__allow_none = 0
self.__allow_long = 0
self.__oldvalue = None
if validator == wx.DefaultValidator:
validator = IntValidator()
wx.TextCtrl.__init__(
self, parent, id, self._toGUI(0),
pos, size, style, validator, name )
# The following lets us set out our "integer update" events:
self.Bind(wx.EVT_TEXT, self.OnText )
# Establish parameters, with appropriate error checking
self.SetBounds(min, max)
self.SetLimited(limited)
self.SetColors(default_color, oob_color)
self.SetNoneAllowed(allow_none)
self.SetLongAllowed(allow_long)
self.SetValue(value)
self.__oldvalue = 0
def OnText( self, event ):
"""
Handles an event indicating that the text control's value
has changed, and issue EVT_INT event.
NOTE: using wx.TextCtrl.SetValue() to change the control's
contents from within a wx.EVT_CHAR handler can cause double
text events. So we check for actual changes to the text
before passing the events on.
"""
value = self.GetValue()
if value != self.__oldvalue:
try:
self.GetEventHandler().ProcessEvent(
IntUpdatedEvent( self.GetId(), self.GetValue(), self ) )
except ValueError:
return
# let normal processing of the text continue
event.Skip()
self.__oldvalue = value # record for next event
def GetValue(self):
"""
Returns the current integer (long) value of the control.
"""
return self._fromGUI( wx.TextCtrl.GetValue(self) )
def SetValue(self, value):
"""
Sets the value of the control to the integer value specified.
The resulting actual value of the control may be altered to
conform with the bounds set on the control if limited,
or colored if not limited but the value is out-of-bounds.
A ValueError exception will be raised if an invalid value
is specified.
"""
wx.TextCtrl.SetValue( self, self._toGUI(value) )
self._colorValue()
def SetMin(self, min=None):
"""
Sets the minimum value of the control. If a value of None
is provided, then the control will have no explicit minimum value.
If the value specified is greater than the current maximum value,
then the function returns 0 and the minimum will not change from
its current setting. On success, the function returns 1.
If successful and the current value is lower than the new lower
bound, if the control is limited, the value will be automatically
adjusted to the new minimum value; if not limited, the value in the
control will be colored with the current out-of-bounds color.
If min > -sys.maxint-1 and the control is configured to not allow longs,
the function will return 0, and the min will not be set.
"""
if( self.__max is None
or min is None
or (self.__max is not None and self.__max >= min) ):
self.__min = min
if self.IsLimited() and min is not None and self.GetValue() < min:
self.SetValue(min)
else:
self._colorValue()
return 1
else:
return 0
def GetMin(self):
"""
Gets the minimum value of the control. It will return the current
minimum integer, or None if not specified.
"""
return self.__min
def SetMax(self, max=None):
"""
Sets the maximum value of the control. If a value of None
is provided, then the control will have no explicit maximum value.
If the value specified is less than the current minimum value, then
the function returns 0 and the maximum will not change from its
current setting. On success, the function returns 1.
If successful and the current value is greater than the new upper
bound, if the control is limited the value will be automatically
adjusted to this maximum value; if not limited, the value in the
control will be colored with the current out-of-bounds color.
If max > sys.maxint and the control is configured to not allow longs,
the function will return 0, and the max will not be set.
"""
if( self.__min is None
or max is None
or (self.__min is not None and self.__min <= max) ):
self.__max = max
if self.IsLimited() and max is not None and self.GetValue() > max:
self.SetValue(max)
else:
self._colorValue()
return 1
else:
return 0
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 0, 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.__limited = limited
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -