import wx import wx.lib.intctrl #---------------------------------------------------------------------- class TestPanel( wx.Panel ): def __init__( self, parent, log ): wx.Panel.__init__( self, parent, -1 ) self.log = log panel = wx.Panel( self, -1 ) self.set_min = wx.CheckBox( panel, -1, "Set minimum value:" ) self.min = wx.lib.intctrl.IntCtrl( panel, size=( 50, -1 ) ) self.min.Enable( False ) self.set_max = wx.CheckBox( panel, -1, "Set maximum value:" ) self.max = wx.lib.intctrl.IntCtrl( panel, size=( 50, -1 ) ) self.max.Enable( False ) self.limit_target = wx.CheckBox( panel, -1, "Limit control" ) self.allow_none = wx.CheckBox( panel, -1, "Allow empty control" ) self.allow_long = wx.CheckBox( panel, -1, "Allow long integers" ) label = wx.StaticText( panel, -1, "Resulting integer control:" ) self.target_ctl = wx.lib.intctrl.IntCtrl( panel ) grid = wx.FlexGridSizer( 0, 2, 0, 0 ) grid.Add( self.set_min, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 ) grid.Add( self.min, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add(self.set_max, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 ) grid.Add( self.max, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( self.limit_target, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( self.allow_none, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( self.allow_long, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 ) grid.Add( label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 ) grid.Add( self.target_ctl, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) outer_box = wx.BoxSizer( wx.VERTICAL ) outer_box.AddSizer( grid, 0, wx.ALIGN_CENTRE|wx.ALL, 20 ) panel.SetAutoLayout( True ) panel.SetSizer( outer_box ) outer_box.Fit( panel ) panel.Move( (50,50) ) self.panel = panel self.Bind(wx.EVT_CHECKBOX, self.OnSetMin, self.set_min) self.Bind(wx.EVT_CHECKBOX, self.OnSetMax, self.set_max) self.Bind(wx.EVT_CHECKBOX, self.SetTargetMinMax, self.limit_target) self.Bind(wx.EVT_CHECKBOX, self.OnSetAllowNone, self.allow_none) self.Bind(wx.EVT_CHECKBOX, self.OnSetAllowLong, self.allow_long) self.Bind(wx.lib.intctrl.EVT_INT, self.SetTargetMinMax, self.min) self.Bind(wx.lib.intctrl.EVT_INT, self.SetTargetMinMax, self.max) self.Bind(wx.lib.intctrl.EVT_INT, self.OnTargetChange, self.target_ctl) def OnSetMin( self, event ): self.min.Enable( self.set_min.GetValue() ) self.SetTargetMinMax() def OnSetMax( self, event ): self.max.Enable( self.set_max.GetValue() ) self.SetTargetMinMax() def SetTargetMinMax( self, event=None ): min = max = None self.target_ctl.SetLimited( self.limit_target.GetValue() ) if self.set_min.GetValue(): min = self.min.GetValue() if self.set_max.GetValue(): max = self.max.GetValue() cur_min, cur_max = self.target_ctl.GetBounds() if min != cur_min and not self.target_ctl.SetMin( min ): self.log.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self.target_ctl.GetMax() ) ) self.min.SetForegroundColour( wx.RED ) else: self.min.SetForegroundColour( wx.BLACK ) self.min.Refresh() if max != cur_max and not self.target_ctl.SetMax( max ): self.log.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self.target_ctl.GetMin() ) ) self.max.SetForegroundColour( wx.RED ) else: self.max.SetForegroundColour( wx.BLACK ) self.max.Refresh() if min != cur_min or max != cur_max: new_min, new_max = self.target_ctl.GetBounds() self.log.write( "current min, max: (%s, %s)\n" % ( str(new_min), str(new_max) ) ) def OnSetAllowNone( self, event ): self.target_ctl.SetNoneAllowed( self.allow_none.GetValue() ) def OnSetAllowLong( self, event ): self.target_ctl.SetLongAllowed( self.allow_long.GetValue() ) def OnTargetChange( self, event ): ctl = event.GetEventObject() value = ctl.GetValue() ib_str = [ " (out of bounds)", "" ] self.log.write( "integer value = %s%s\n" % ( str(value), ib_str[ ctl.IsInBounds(value) ] ) ) #---------------------------------------------------------------------- def runTest( frame, nb, log ): win = TestPanel( nb, log ) return win #---------------------------------------------------------------------- overview = """

IntCtrl provides a control that takes and returns integers as value, and provides bounds support and optional value limiting.

Here's the API for IntCtrl:

    IntCtrl(
         parent, id = -1,
         value = 0,
         min = None,
         max = None,
         limited = False,
         allow_none = False,
         allow_long = False,
         default_color = wxBLACK,
         oob_color = wxRED,
         pos = wxDefaultPosition,
         size = wxDefaultSize,
         style = 0,
         name = "integer")


EVT_INT(win, id, func)
Respond to a wxEVT_COMMAND_INT_UPDATED event, generated when the value changes. Notice that this event will always be sent when the control's contents changes - whether this is due to user input or comes from the program itself (for example, if SetValue() is called.)

SetValue(int)
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.
GetValue()
Retrieves the integer value from the control. The value retrieved will be sized as an int if possible or a long, if necessary.

SetMin(min=None)
Sets the expected minimum value, or lower bound, of the control. (The lower bound will only be enforced if the control is configured to limit its values to the set bounds.) If a value of None is provided, then the control will have no explicit lower bound. If the value specified is greater than the current lower bound, then the function returns 0 and the lower bound 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.
GetMin()
Gets the current lower bound value for the control. It will return None if no lower bound is currently specified.

SetMax(max=None)
Sets the expected maximum value, or upper bound, of the control. (The upper bound will only be enforced if the control is configured to limit its values to the set bounds.) If a value of None is provided, then the control will have no explicit upper bound. If the value specified is less than the current lower bound, 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 the new maximum value; if not limited, the value in the control will be colored with the current out-of-bounds color.
GetMax()
Gets the current upper bound value for the control. It will return None if no upper bound is currently specified.

SetBounds(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.
GetBounds()
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.

IsInBounds(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.

SetLimited(bool)
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.
IsLimited()
Returns True if the control is currently limiting the value to fall within the current bounds.

SetNoneAllowed(bool)
If called with a value of True, this function will cause the control to allow the value to be empty, representing a value of None. If called with a value of false, this function will prevent the value from being None. If the value of the control is currently None, ie. the control is empty, then the value will be changed to that of the lower bound of the control, or 0 if no lower bound is set.
IsNoneAllowed()
Returns True if the control currently allows its value to be None.

SetLongAllowed(bool)
If called with a value of True, this function will cause the control to allow the value to be a long. If called with a value of False, and the value of the control is currently a long value, 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.
IsLongAllowed()
Returns True if the control currently allows its value to be of type long.

SetColors(default_color=wxBLACK, oob_color=wxRED)
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.
GetColors()
Returns a tuple of (default_color, oob_color) indicating the current color settings for the control.

Cut()
Will allow cuts to the clipboard of the text portion of the value, leaving the value of zero if the entire contents are "cut."
Paste()
Will paste the contents of the clipboard to the selected portion of the value; if the resulting string does not represent a legal value, a ValueError will result. If the result is out-of bounds, it will either be adjusted or colored as appropriate.
""" if __name__ == '__main__': import sys,os import run run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])