Gtk.SpinButton¶
class — extends Widget, Accessible, AccessibleRange, Buildable, CellEditable, ConstraintTarget, Editable, Orientable
Allows to enter or change numeric values.
<picture> <source srcset="spinbutton-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkSpinButton" src="spinbutton.png"> </picture>
Rather than having to directly type a number into a GtkEntry,
GtkSpinButton allows the user to click on one of two arrows
to increment or decrement the displayed value. A value can still be
typed in, with the bonus that it can be checked to ensure it is in a
given range.
The main properties of a GtkSpinButton are through an adjustment.
See the Adjustment documentation for more details about
an adjustment's properties.
Note that GtkSpinButton will by default make its entry large enough
to accommodate the lower and upper bounds of the adjustment. If this
is not desired, the automatic sizing can be turned off by explicitly
setting Editable.width-chars to a value != -1.
Using a GtkSpinButton to get an integer¶
// Provides a function to retrieve an integer value from a GtkSpinButton
// and creates a spin button to model percentage values.
int
grab_int_value (GtkSpinButton *button,
gpointer user_data)
{
return gtk_spin_button_get_value_as_int (button);
}
void
create_integer_spin_button (void)
{
GtkWidget *window, *button;
GtkAdjustment *adjustment;
adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);
window = gtk_window_new ();
// creates the spinbutton, with no decimal places
button = gtk_spin_button_new (adjustment, 1.0, 0);
gtk_window_set_child (GTK_WINDOW (window), button);
gtk_window_present (GTK_WINDOW (window));
}
Using a GtkSpinButton to get a floating point value¶
// Provides a function to retrieve a floating point value from a
// GtkSpinButton, and creates a high precision spin button.
float
grab_float_value (GtkSpinButton *button,
gpointer user_data)
{
return gtk_spin_button_get_value (button);
}
void
create_floating_spin_button (void)
{
GtkWidget *window, *button;
GtkAdjustment *adjustment;
adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);
window = gtk_window_new ();
// creates the spinbutton, with three decimal places
button = gtk_spin_button_new (adjustment, 0.001, 3);
gtk_window_set_child (GTK_WINDOW (window), button);
gtk_window_present (GTK_WINDOW (window));
}
Shortcuts and Gestures¶
The following signals have default keybindings:
CSS nodes¶
spinbutton.horizontal
├── text
│ ├── undershoot.left
│ ╰── undershoot.right
├── button.down
╰── button.up
spinbutton.vertical
├── button.up
├── text
│ ├── undershoot.left
│ ╰── undershoot.right
╰── button.down
GtkSpinButtons main CSS node has the name spinbutton. It creates subnodes
for the entry and the two buttons, with these names. The button nodes have
the style classes .up and .down. The GtkText subnodes (if present) are put
below the text node. The orientation of the spin button is reflected in
the .vertical or .horizontal style class on the main node.
Accessibility¶
GtkSpinButton uses the AccessibleRole.spin_button role.
Constructors¶
new¶
Creates a new GtkSpinButton.
Parameters:
adjustment— theGtkAdjustmentthat this spin button should useclimb_rate— specifies by how much the rate of change in the value will accelerate if you continue to hold down an up/down button or arrow keydigits— the number of decimal places to display
new_with_range¶
Creates a new GtkSpinButton with the given properties.
This is a convenience constructor that allows creation
of a numeric GtkSpinButton without manually creating
an adjustment. The value is initially set to the minimum
value and a page increment of 10 * step is the default.
The precision of the spin button is equivalent to the
precision of step.
Note that the way in which the precision is derived works
best if step is a power of ten. If the resulting precision
is not suitable for your needs, use
SpinButton.set_digits to correct it.
Parameters:
min— Minimum allowable valuemax— Maximum allowable valuestep— Increment added or subtracted by spinning the widget
Methods¶
configure¶
Changes the properties of an existing spin button.
The adjustment, climb rate, and number of decimal places are updated accordingly.
Parameters:
adjustment— aGtkAdjustmentto replace the spin button’s existing adjustment, orNoneto leave its current adjustment unchangedclimb_rate— the new climb ratedigits— the number of decimal places to display in the spin button
get_activates_default¶
Retrieves the value set by SpinButton.set_activates_default.
get_adjustment¶
Get the adjustment associated with a GtkSpinButton.
get_climb_rate¶
Returns the acceleration rate for repeated changes.
get_digits¶
Fetches the precision of spin_button.
get_increments¶
Gets the current step and page the increments
used by spin_button.
See SpinButton.set_increments.
get_numeric¶
Returns whether non-numeric text can be typed into the spin button.
get_range¶
Gets the range allowed for spin_button.
See SpinButton.set_range.
get_snap_to_ticks¶
Returns whether the values are corrected to the nearest step.
get_update_policy¶
Gets the update behavior of a spin button.
See SpinButton.set_update_policy.
get_value¶
Get the value in the spin_button.
get_value_as_int¶
Get the value spin_button represented as an integer.
get_wrap¶
Returns whether the spin button’s value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.
set_activates_default¶
Sets whether activating the spin button will activate the default widget for the window containing the spin button.
See SpinButton.activate for what counts as activation.
Parameters:
activates_default—Trueto activate window’s default widget on activation
set_adjustment¶
Replaces the GtkAdjustment associated with spin_button.
Parameters:
adjustment— aGtkAdjustmentto replace the existing adjustment
set_climb_rate¶
Sets the acceleration rate for repeated changes when you hold down a button or key.
Parameters:
climb_rate— the rate of acceleration, must be >= 0
set_digits¶
Set the precision to be displayed by spin_button.
Up to 20 digit precision is allowed.
Parameters:
digits— the number of digits after the decimal point to be displayed for the spin button’s value
set_increments¶
Sets the step and page increments for spin_button.
This affects how quickly the value changes when the spin button’s arrows are activated.
Parameters:
step— increment applied for a button 1 press.page— increment applied for a button 2 press.
set_numeric¶
Sets the flag that determines if non-numeric text can be typed into the spin button.
Parameters:
numeric— flag indicating if only numeric entry is allowed
set_range¶
Sets the minimum and maximum allowable values for spin_button.
If the current value is outside this range, it will be adjusted to fit within the range, otherwise it will remain unchanged.
Parameters:
min— minimum allowable valuemax— maximum allowable value
set_snap_to_ticks¶
Sets the policy as to whether values are corrected to the nearest step increment when a spin button is activated after providing an invalid value.
Parameters:
snap_to_ticks— a flag indicating if invalid values should be corrected
set_update_policy¶
Sets the update behavior of a spin button.
This determines whether the spin button is always updated or only when a valid value is set.
Parameters:
policy— aGtkSpinButtonUpdatePolicyvalue
set_value¶
Sets the value of spin_button.
Parameters:
value— the new value
set_wrap¶
Sets the flag that determines if a spin button value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.
Parameters:
wrap— a flag indicating if wrapping behavior is performed
spin¶
Increment or decrement a spin button’s value in a specified direction by a specified amount.
Parameters:
direction— aGtkSpinTypeindicating the direction to spinincrement— step increment to apply in the specified direction
update¶
Manually force an update of the spin button.
Properties¶
activates_default¶
Whether to activate the default widget when the spin button is activated.
See SpinButton.activate for what counts as activation.
adjustment¶
The adjustment that holds the value of the spin button.
climb_rate¶
The acceleration rate when you hold down a button or key.
digits¶
The number of decimal places to display.
numeric¶
Whether non-numeric characters should be ignored.
snap_to_ticks¶
Whether erroneous values are automatically changed to the spin buttons nearest step increment.
update_policy¶
Whether the spin button should update always, or only when the value is acceptable.
value¶
The current value.
wrap¶
Whether a spin button should wrap upon reaching its limits.
Signals¶
activate¶
Emitted when the spin button is activated.
The keybindings for this signal are all forms of the <kbd>Enter</kbd> key.
If the <kbd>Enter</kbd> key results in the value being committed to the spin button, then activation does not occur until <kbd>Enter</kbd> is pressed again.
change-value¶
Emitted when the user initiates a value change.
This is a keybinding signal.
Applications should not connect to it, but may emit it with g_signal_emit_by_name() if they need to control the cursor programmatically.
The default bindings for this signal are Up/Down and PageUp/PageDown.
input¶
Emitted to convert the users input into a double value.
The signal handler is expected to use Editable.get_text
to retrieve the text of the spinbutton and set new_value to the
new value.
The default conversion uses GLib.strtod.
output¶
Emitted to tweak the formatting of the value for display.
// show leading zeros
static gboolean
on_output (GtkSpinButton *spin,
gpointer data)
{
char *text;
int value;
value = gtk_spin_button_get_value_as_int (spin);
text = g_strdup_printf ("%02d", value);
gtk_editable_set_text (GTK_EDITABLE (spin), text):
g_free (text);
return TRUE;
}
value-changed¶
Emitted when the value is changed.
Also see the SpinButton.output signal.
wrapped¶
Emitted right after the spinbutton wraps from its maximum to its minimum value or vice-versa.