📄 intctrl.py
字号:
if limited:
min = self.GetMin()
max = self.GetMax()
if not min is None and self.GetValue() < min:
self.SetValue(min)
elif not max is None and self.GetValue() > max:
self.SetValue(max)
else:
self._colorValue()
def IsLimited(self):
"""
Returns True if the control is currently limiting the
value to fall within the current bounds.
"""
return self.__limited
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.
"""
if value is None:
value = self.GetValue()
if( not (value is None and self.IsNoneAllowed())
and type(value) not in (types.IntType, types.LongType) ):
raise ValueError (
'IntCtrl requires integer values, passed %s'% repr(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):
return 0
else:
return min <= value <= max
def SetNoneAllowed(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 0, the
value of the control will be set to the minimum value of the
control, or 0 if no lower bound is set.
"""
self.__allow_none = 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.__allow_none
def SetLongAllowed(self, allow_long):
"""
Change the behavior of the validation code, allowing control
to have a long value or not, as appropriate. If the value
of the control is currently long, and allow_long is 0, the
value of the control will be adjusted to fall within the
size of an integer type, at either the sys.maxint or -sys.maxint-1,
for positive and negative values, respectively.
"""
current_value = self.GetValue()
if not allow_long and type(current_value) is types.LongType:
if current_value > 0:
self.SetValue(MAXINT)
else:
self.SetValue(MININT)
self.__allow_long = allow_long
def IsLongAllowed(self):
return self.__allow_long
def SetColors(self, default_color=wx.BLACK, oob_color=wx.RED):
"""
Tells the control what colors to use for normal and out-of-bounds
values. If the value currently exceeds the bounds, it will be
recolored accordingly.
"""
self.__default_color = default_color
self.__oob_color = oob_color
self._colorValue()
def GetColors(self):
"""
Returns a tuple of (default_color, oob_color), indicating
the current color settings for the control.
"""
return self.__default_color, self.__oob_color
def _colorValue(self, value=None):
"""
Colors text with oob_color if current value exceeds bounds
set for control.
"""
if not self.IsInBounds(value):
self.SetForegroundColour(self.__oob_color)
else:
self.SetForegroundColour(self.__default_color)
self.Refresh()
def _toGUI( self, value ):
"""
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.
"""
if value is None and self.IsNoneAllowed():
return ''
elif type(value) == types.LongType and not self.IsLongAllowed():
raise ValueError (
'IntCtrl requires integer value, passed long' )
elif type(value) not in (types.IntType, types.LongType):
raise ValueError (
'IntCtrl requires integer value, passed %s'% repr(value) )
elif self.IsLimited():
min = self.GetMin()
max = self.GetMax()
if not min is None and value < min:
raise ValueError (
'value is below minimum value of control %d'% value )
if not max is None and value > max:
raise ValueError (
'value exceeds value of control %d'% value )
return str(value)
def _fromGUI( self, value ):
"""
Conversion function used in getting the value of the control.
"""
# 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 == '':
if not self.IsNoneAllowed():
return 0
else:
return None
else:
try:
return int( value )
except ValueError:
if self.IsLongAllowed():
return long( value )
else:
raise
def Cut( self ):
"""
Override the wxTextCtrl's .Cut function, with our own
that does validation. Will result in a value of 0
if entire contents of control are removed.
"""
sel_start, sel_to = self.GetSelection()
select_len = sel_to - sel_start
textval = wx.TextCtrl.GetValue(self)
do = wx.TextDataObject()
do.SetText(textval[sel_start:sel_to])
wx.TheClipboard.Open()
wx.TheClipboard.SetData(do)
wx.TheClipboard.Close()
if select_len == len(wxTextCtrl.GetValue(self)):
if not self.IsNoneAllowed():
self.SetValue(0)
self.SetInsertionPoint(0)
self.SetSelection(0,1)
else:
self.SetValue(None)
else:
new_value = self._fromGUI(textval[:sel_start] + textval[sel_to:])
self.SetValue(new_value)
def _getClipboardContents( self ):
"""
Subroutine for getting the current contents of the clipboard.
"""
do = wx.TextDataObject()
wx.TheClipboard.Open()
success = wx.TheClipboard.GetData(do)
wx.TheClipboard.Close()
if not success:
return None
else:
# Remove leading and trailing spaces before evaluating contents
return do.GetText().strip()
def Paste( self ):
"""
Override the wxTextCtrl's .Paste function, with our own
that does validation. Will raise ValueError if not a
valid integerizable value.
"""
paste_text = self._getClipboardContents()
if paste_text:
# (conversion will raise ValueError if paste isn't legal)
sel_start, sel_to = self.GetSelection()
text = wx.TextCtrl.GetValue( self )
new_text = text[:sel_start] + paste_text + text[sel_to:]
if new_text == '' and self.IsNoneAllowed():
self.SetValue(None)
else:
value = self._fromGUI(new_text)
self.SetValue(value)
new_pos = sel_start + len(paste_text)
wx.CallAfter(self.SetInsertionPoint, new_pos)
#===========================================================================
if __name__ == '__main__':
import traceback
class myDialog(wx.Dialog):
def __init__(self, parent, id, title,
pos = wx.DefaultPosition, size = wx.DefaultSize,
style = wx.DEFAULT_DIALOG_STYLE ):
wx.Dialog.__init__(self, parent, id, title, pos, size, style)
self.int_ctrl = IntCtrl(self, wx.NewId(), size=(55,20))
self.OK = wx.Button( self, wx.ID_OK, "OK")
self.Cancel = wx.Button( self, wx.ID_CANCEL, "Cancel")
vs = wx.BoxSizer( wx.VERTICAL )
vs.Add( self.int_ctrl, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
hs = wx.BoxSizer( wx.HORIZONTAL )
hs.Add( self.OK, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
hs.Add( self.Cancel, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
vs.Add(hs, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
self.SetAutoLayout( True )
self.SetSizer( vs )
vs.Fit( self )
vs.SetSizeHints( self )
self.Bind(EVT_INT, self.OnInt, self.int_ctrl)
def OnInt(self, event):
print 'int now', event.GetValue()
class TestApp(wx.App):
def OnInit(self):
try:
self.frame = wx.Frame(None, -1, "Test", (20,20), (120,100) )
self.panel = wx.Panel(self.frame, -1)
button = wx.Button(self.panel, 10, "Push Me", (20, 20))
self.Bind(wx.EVT_BUTTON, self.OnClick, button)
except:
traceback.print_exc()
return False
return True
def OnClick(self, event):
dlg = myDialog(self.panel, -1, "test IntCtrl")
dlg.int_ctrl.SetValue(501)
dlg.int_ctrl.SetInsertionPoint(1)
dlg.int_ctrl.SetSelection(1,2)
rc = dlg.ShowModal()
print 'final value', dlg.int_ctrl.GetValue()
del dlg
self.frame.Destroy()
def Show(self):
self.frame.Show(True)
try:
app = TestApp(0)
app.Show()
app.MainLoop()
except:
traceback.print_exc()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -