Gtk.SpinButton


Gtk.SpinButton — Retrieve an integer or floating-point number from the user

Object Hierarchy:

    GObject
    ╰── GInitiallyUnowned
        ╰── Gtk.Widget
            ╰── Gtk.Entry
                ╰── Gtk.SpinButton

See also:

Gtk.Entry


Functions:


Signals:

  • “change-value” (spin_button, scroll, user_data)
  • “input” (spin_button, new_value, user_data)
  • “output” (spin_button, user_data)
  • “value-changed” (spin_button, user_data)

Description:

A Gtk.SpinButton is an ideal way to allow the user to set the value of some attribute. Rather than having to directly type a number into a Gtk.Entry, Gtk.SpinButton 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 Gtk.SpinButton are through an adjustment.

See the Gtk.Adjustment section for more details about an adjustment's properties.

Using a Gtk.SpinButton to get an integer:
    def add_spin_button(self, parent):
        adjustment = Gtk.Adjustment(value=0,
                                    lower=-10,
                                    upper=25,
                                    step_increment=1,
                                    page_increment=5,
                                    page_size=0)
        spinbutton = Gtk.SpinButton(adjustment=adjustment)
        spinbutton.connect("value-changed", self.on_spinbutton_changed)
        parent.add(spinbutton)

    def on_spinbutton_changed(self, spinbutton):
        print("SpinButton value: %i" % (spinbutton.get_value_as_int()))
Using a Gtk.SpinButton to get a floating point value:
    def add_spin_button(self, parent):
        adjustment = Gtk.Adjustment(value=0.1,
                                    lower=-10.0,
                                    upper=20.0,
                                    step_increment=0.1,
                                    page_increment=0.5,
                                    page_size=0.0)
        spinbutton = Gtk.SpinButton(adjustment=adjustment, climb_rate=1.0, digits=2)
        spinbutton.connect("value-changed", self.on_spinbutton_changed)
        self.add(spinbutton)

    def on_spinbutton_changed(self, spinbutton):
        print("SpinButton value: %f" % (spinbutton.get_value()))

Function Details:

configure()

configure (self, adjustment:Gtk.Adjustment=None, climb_rate:float, digits:int)

Changes the properties of an existing spin button. The adjustment,climb rate, and number of decimal places are all changed accordingly,after this function call.


new()

new (adjustment:Gtk.Adjustment=None, climb_rate:float, digits:int) -> Gtk.Widget

Creates a new Gtk.SpinButton.

  • Returns: The new spin button as a Gtk.Widget

new_with_range()

new_with_range (min:float, max:float, step:float) -> Gtk.Widget

This is a convenience constructor that allows creation of a numericGtk.SpinButton without manually creating an adjustment. The value isinitially set to the minimum value and a page increment of 10 * stepis the default. The precision of the spin button is equivalent to theprecision of step. Note that the way in which the precision is derived works best if stepis a power of ten. If the resulting precision is not suitable for yourneeds, use Gtk.SpinButton:set_digits() to correct it.

  • Returns: The new spin button as a Gtk.Widget

set_adjustment()

set_adjustment (self, adjustment:Gtk.Adjustment)

Replaces the Gtk.Adjustment associated with spin_button.


get_adjustment()

get_adjustment (self) -> Gtk.Adjustment

Get the adjustment associated with a Gtk.SpinButton

  • Returns: the Gtk.Adjustment of spin_button.

set_digits()

set_digits (self, digits:int)

Set the precision to be displayed by spin_button. Up to 20 digit precisionis allowed.


set_increments()

set_increments (self, step:float, page:float)

Sets the step and page increments for spin_button. This affects howquickly the value changes when the spin button’s arrows are activated.


set_range()

set_range (self, min:float, max:float)

Sets the minimum and maximum allowable values for spin_button. If the current value is outside this range, it will be adjustedto fit within the range, otherwise it will remain unchanged.


get_value_as_int()

get_value_as_int (self) -> int

Get the value spin_button represented as an integer.

  • Returns: the value of spin_button

set_value()

set_value (self, value:float)

Sets the value of spin_button.


set_update_policy()

set_update_policy (self, policy:Gtk.SpinButtonUpdatePolicy)

Sets the update behavior of a spin button.This determines whether the spin button is always updatedor only when a valid value is set.


set_numeric()

set_numeric (self, numeric:bool)

Sets the flag that determines if non-numeric text can be typedinto the spin button.


spin()

spin (self, direction:Gtk.SpinType, increment:float)

Increment or decrement a spin button’s value in a specifieddirection by a specified amount.


set_wrap()

set_wrap (self, wrap:bool)

Sets the flag that determines if a spin button value wrapsaround to the opposite limit when the upper or lower limitof the range is exceeded.


set_snap_to_ticks()

set_snap_to_ticks (self, snap_to_ticks:bool)

Sets the policy as to whether values are corrected to thenearest step increment when a spin button is activated afterproviding an invalid value.


update()

update (self)

Manually force an update of the spin button.


get_digits()

get_digits (self) -> int

Fetches the precision of spin_button. See Gtk.SpinButton:set_digits().

  • Returns: the current precision

get_increments()

get_increments (self) -> step:float, page:float

Gets the current step and page the increments used by spin_button. SeeGtk.SpinButton:set_increments().


get_numeric()

get_numeric (self) -> bool

Returns whether non-numeric text can be typed into the spin button.See Gtk.SpinButton:set_numeric().

  • Returns: True if only numeric text can be entered

get_range()

get_range (self) -> min:float, max:float

Gets the range allowed for spin_button.See Gtk.SpinButton:set_range().


get_snap_to_ticks()

get_snap_to_ticks (self) -> bool

Returns whether the values are corrected to the nearest step.See Gtk.SpinButton:set_snap_to_ticks().

  • Returns: True if values are snapped to the nearest step

get_update_policy()

get_update_policy (self) -> Gtk.SpinButtonUpdatePolicy

Gets the update behavior of a spin button.See Gtk.SpinButton:set_update_policy().

  • Returns: the current update policy

get_value()

get_value (self) -> float

Get the value in the spin_button.

  • Returns: the value of spin_button

get_wrap()

get_wrap (self) -> bool

Returns whether the spin button’s value wraps around to theopposite limit when the upper or lower limit of the range isexceeded. See Gtk.SpinButton:set_wrap().

  • Returns: True if the spin button wraps around

Example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class SpinButton(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        adjustment = Gtk.Adjustment(value=0,
                                    lower=-10,
                                    upper=25,
                                    step_increment=1,
                                    page_increment=5,
                                    page_size=0)
        spinbutton = Gtk.SpinButton(adjustment=adjustment)
        spinbutton.connect("value-changed", self.on_spinbutton_changed)
        self.add(spinbutton)

    def on_spinbutton_changed(self, spinbutton):
        print("SpinButton value: %i" % (spinbutton.get_value_as_int()))

window = SpinButton()
window.show_all()

Gtk.main()

results matching ""

    No results matching ""