from wxPython.wx import * from wxPython.lib.intctrl import * #---------------------------------------------------------------------- class TestPanel( wxPanel ): def __init__( self, parent, log ): wxPanel.__init__( self, parent, -1 ) self.log = log panel = wxPanel( self, -1 ) self.set_min = wxCheckBox( panel, -1, "Set minimum value:" ) self.min = wxIntCtrl( panel, size=wxSize( 50, -1 ) ) self.min.Enable( False ) self.set_max = wxCheckBox( panel, -1, "Set maximum value:" ) self.max = wxIntCtrl( panel, size=wxSize( 50, -1 ) ) self.max.Enable( False ) self.limit_target = wxCheckBox( panel, -1, "Limit control" ) self.allow_none = wxCheckBox( panel, -1, "Allow empty control" ) self.allow_long = wxCheckBox( panel, -1, "Allow long integers" ) label = wxStaticText( panel, -1, "Resulting integer control:" ) self.target_ctl = wxIntCtrl( panel ) grid = wxFlexGridSizer( 0, 2, 0, 0 ) grid.AddWindow( self.set_min, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5 ) grid.AddWindow( self.min, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddWindow( self.set_max, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5 ) grid.AddWindow( self.max, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddWindow( self.limit_target, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddWindow( self.allow_none, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddWindow( self.allow_long, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddSpacer( 20, 0, 0, wxALIGN_LEFT|wxALL, 5 ) grid.AddWindow( label, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5 ) grid.AddWindow( self.target_ctl, 0, wxALIGN_LEFT|wxALL, 5 ) outer_box = wxBoxSizer( wxVERTICAL ) outer_box.AddSizer( grid, 0, wxALIGN_CENTRE|wxALL, 20 ) panel.SetAutoLayout( True ) panel.SetSizer( outer_box ) outer_box.Fit( panel ) panel.Move( (50,50) ) self.panel = panel EVT_CHECKBOX( self, self.set_min.GetId(), self.OnSetMin ) EVT_CHECKBOX( self, self.set_max.GetId(), self.OnSetMax ) EVT_CHECKBOX( self, self.limit_target.GetId(), self.SetTargetMinMax ) EVT_CHECKBOX( self, self.allow_none.GetId(), self.OnSetAllowNone ) EVT_CHECKBOX( self, self.allow_long.GetId(), self.OnSetAllowLong ) EVT_INT( self, self.min.GetId(), self.SetTargetMinMax ) EVT_INT( self, self.max.GetId(), self.SetTargetMinMax ) EVT_INT( self, self.target_ctl.GetId(), self.OnTargetChange ) 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( wxRED ) else: self.min.SetForegroundColour( wxBLACK ) 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( wxRED ) else: self.max.SetForegroundColour( wxBLACK ) 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 = """

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

Here's the API for wxIntCtrl:

    wxIntCtrl(
         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 fakse, 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])])