SpinButton

A SpinButton allows the user to select a value from a range of numeric values. It has an Entry widget with up and down arrow buttons at the side. Clicking the buttons causes the value to 'spin' up and down across the range of possible values. The Entry widget may also be used to enter a value directly.

The value can have an adjustable number of decimal places, and the step size is configurable. SpinButtons have an 'auto-repeat' feature as well: holding down one of the arrows can optionally cause the value to change more quickly the longer the arrow is held down.

SpinButtons use an Adjustment object to hold information about the range of values. These Adjustment attributes are used by the Spin Button like so:

Additionally, mouse button 3 can be used to jump directly to the upper or lower values.

The SpinButton can create a default Adjustment, which you can access via the get_adjustment() method, or you can specify an existing Adjustment in the constructor.

Methods

The number of decimal places can be altered using the set_digits() method.

You can set the spinbutton's value using the set_value() method, and retrieve it with get_value().

The spin() method 'spins' the SpinButton, as if one of its arrows had been clicked. You need to specify a Gtk::SpinType to specify the direction or new position.

To prevent the user from typing non-numeric characters into the entry box, pass true to the set_numeric() method.

To make the SpinButton 'wrap' between its upper and lower bounds, use the set_wrap() method.

To force it to snap to the nearest step_increment, use set_snap_to_ticks

You can modify the update policy using the set_update_policy method, specifying either Gtk::UPDATE_ALWAYS or Gtk::UPDATE_IF_VALID. Gtk::UPDATE_ALWAYS causes the SpinButton to ignore errors encountered while converting the text in the entry box to a numeric value. This setting also therefore allows the SpinButton to accept non-numeric values. You can force an immediate update using the update() method.

Reference

Example

Here's an example of a SpinButton in action:

Figure 6.4. SpinButton

Source Code