Gtk.Scale


Gtk.Scale — A slider widget for selecting a value from a range

Object Hierarchy:

    GObject
    ╰── GInitiallyUnowned
        ╰── Gtk.Widget
            ╰── Gtk.Range
                ╰── Gtk.Scale
                    ├── Gtk.HScale
                    ╰── Gtk.VScale

Functions:


Signals:

  • “format-value” (scale, value, user_data)

Description:

A Gtk.Scale is a slider control used to select a numeric value.

To use it, you’ll probably want to investigate the methods on its base class, GtkRange, in addition to the methods for Gtk.Scale itself.

To set the value of a scale, you would normally use Gtk.Range::set_value().

To detect changes to the value, you would normally use the “value-changed” signal.

Note that using the same upper and lower bounds for the Gtk.Scale (through the Gtk.Range methods) will hide the slider itself. This is useful for applications that want to show an undeterminate value on the scale, without changing the layout of the application (such as movie or music players).

Gtk.Scale as Gtk.Buildable:

Gtk.Scale supports a custom \ element, which can contain multiple \ elements. The value and position attributes have the same meaning as Gtk.Scale::add_mark() parameters of the same name. If the element is not empty, its content is taken as the markup to show at the mark. It can be translated with the usual translatable and context attributes.


Function Details:

new()

new (orientation:Gtk.Orientation, adjustment:Gtk.Adjustment=None) -> Gtk.Widget

Creates a new Gtk.Scale.

  • Returns: a new Gtk.Scale

  • Since: 3.0


new_with_range()

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

Creates a new scale widget with the given orientation that lets theuser input a number between min and max (including min and max)with the increment step. step must be nonzero; it’s the distancethe slider moves when using the arrow keys to adjust the scalevalue. 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.Scale:set_digits() to correct it.

  • Returns: a new Gtk.Scale

  • Since: 3.0


set_digits()

set_digits (self, digits:int)

Sets the number of decimal places that are displayed in the value.Also causes the value of the adjustment to be rounded off to thisnumber of digits, so the retrieved value matches the value the user saw.


set_draw_value()

set_draw_value (self, draw_value:bool)

Specifies whether the current value is displayed as a string next to the slider.


set_has_origin()

set_has_origin (self, has_origin:bool)

If has_origin is set to True (the default),the scale will highlight the part of the scalebetween the origin (bottom or left side) of the scaleand the current value.

  • Since: 3.4

set_value_pos()

set_value_pos (self, pos:Gtk.PositionType)

Sets the position in which the current value is displayed.


get_digits()

get_digits (self) -> int

Gets the number of decimal places that are displayed in the value.

  • Returns: the number of decimal places that are displayed

get_draw_value()

get_draw_value (self) -> bool

Returns whether the current value is displayed as a string next to the slider.

  • Returns: whether the current value is displayed as a string

get_has_origin()

get_has_origin (self) -> bool

Returns whether the scale has an origin.

  • Returns: True if the scale has an origin.

  • Since: 3.4


get_value_pos()

get_value_pos (self) -> Gtk.PositionType

Gets the position in which the current value is displayed.

  • Returns: the position in which the current value is displayed

get_layout()

get_layout (self) -> Pango.Layout

Gets the PangoLayout used to display the scale. The returnedobject is owned by the scale so does not need to be freed bythe caller.

  • Returns: the PangoLayout for this scale,or None if the “draw-value” property is FALSE.

  • Since: 2.4


get_layout_offsets()

get_layout_offsets (self) -> x:int, y:int

Obtains the coordinates where the scale will draw the PangoLayout representing the text in the scale. Rememberwhen using the PangoLayout function you need to convert toand from pixels using PANGO_PIXELS() or PANGO_SCALE. If the “draw-value” property is FALSE, the return values are undefined.

  • Since: 2.4

add_mark()

add_mark (self, value:float, position:Gtk.PositionType, markup:str=None)

Adds a mark at value. A mark is indicated visually by drawing a tick mark next to the scale,and GTK+ makes it easy for the user to position the scale exactly at themarks value. If markup is not None, text is shown next to the tick mark. To remove marks from a scale, use Gtk.Scale:clear_marks().

  • Since: 2.16

clear_marks()

clear_marks (self)

Removes any marks that have been added with Gtk.Scale:add_mark().

  • Since: 2.16

Example:

#!/usr/bin/env python

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

class Scale(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)
        self.connect("destroy", Gtk.main_quit)

        grid = Gtk.Grid()
        self.add(grid)

        value = random.randint(0, 100)
        adjustment = Gtk.Adjustment(value, 0, 100, 1, 10, 0)

        self.scale = Gtk.Scale(orientation=Gtk.Orientation.VERTICAL, adjustment=adjustment)
        self.scale.set_value_pos(Gtk.PositionType.BOTTOM)
        self.scale.set_vexpand(True)
        self.scale.set_hexpand(True)
        grid.attach(self.scale, 0, 0, 2, 1)

        buttonAdd = Gtk.Button(label="Add Mark")
        buttonAdd.connect("clicked", self.on_add_mark_clicked)
        grid.attach(buttonAdd, 0, 1, 1, 1)

        buttonClear = Gtk.Button(label="Clear Marks")
        buttonClear.connect("clicked", self.on_clear_marks_clicked)
        grid.attach(buttonClear, 1, 1, 1, 1)

        radiobuttonVertical = Gtk.RadioButton(group=None, label="Vertical Scale")
        radiobuttonVertical.orientation = 0
        radiobuttonVertical.connect("toggled", self.on_orientation_clicked)
        grid.attach(radiobuttonVertical, 0, 3, 2, 1)

        radiobuttonHorizontal = Gtk.RadioButton(group=radiobuttonVertical, label="Horizontal Scale")
        radiobuttonHorizontal.orientation = 1
        radiobuttonHorizontal.connect("toggled", self.on_orientation_clicked)
        grid.attach(radiobuttonHorizontal, 0, 2, 2, 1)

    def on_add_mark_clicked(self, button):
        value = self.scale.get_value()
        self.scale.add_mark(value, Gtk.PositionType.LEFT, "Mark")

    def on_clear_marks_clicked(self, button):
        self.scale.clear_marks()

    def on_orientation_clicked(self, radiobutton):
        if radiobutton.orientation == 0:
            self.scale.set_orientation(Gtk.Orientation.VERTICAL)
        else:
            self.scale.set_orientation(Gtk.Orientation.HORIZONTAL)

window = Scale()
window.show_all()

Gtk.main()

results matching ""

    No results matching ""