Skip to content

Gtk.Scale

class — extends Range, Accessible, AccessibleRange, Buildable, ConstraintTarget, Orientable

Allows to select a numeric value with a slider control.

<picture> <source srcset="scales-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkScale" src="scales.png"> </picture>

To use it, you’ll probably want to investigate the methods on its base class, Range, in addition to the methods for GtkScale itself. To set the value of a scale, you would normally use Range.set_value. To detect changes to the value, you would normally use the Range.value-changed signal.

Note that using the same upper and lower bounds for the GtkScale (through the GtkRange 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).

GtkScale as GtkBuildable

GtkScale supports a custom <marks> element, which can contain multiple <mark\> elements. The “value” and “position” attributes have the same meaning as 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.

Shortcuts and Gestures

GtkPopoverMenu supports the following keyboard shortcuts:

  • Arrow keys, <kbd>+</kbd> and <kbd>-</kbd> will increment or decrement by step, or by page when combined with <kbd>Ctrl</kbd>.
  • <kbd>PgUp</kbd> and <kbd>PgDn</kbd> will increment or decrement by page.
  • <kbd>Home</kbd> and <kbd>End</kbd> will set the minimum or maximum value.

CSS nodes

scale[.fine-tune][.marks-before][.marks-after]
├── [value][.top][.right][.bottom][.left]
├── marks.top
│   ├── mark
│   ┊    ├── [label]
│   ┊    ╰── indicator
┊   ┊
│   ╰── mark
├── marks.bottom
│   ├── mark
│   ┊    ├── indicator
│   ┊    ╰── [label]
┊   ┊
│   ╰── mark
╰── trough
    ├── [fill]
    ├── [highlight]
    ╰── slider

GtkScale has a main CSS node with name scale and a subnode for its contents, with subnodes named trough and slider.

The main node gets the style class .fine-tune added when the scale is in 'fine-tuning' mode.

If the scale has an origin (see Scale.set_has_origin), there is a subnode with name highlight below the trough node that is used for rendering the highlighted part of the trough.

If the scale is showing a fill level (see Range.set_show_fill_level), there is a subnode with name fill below the trough node that is used for rendering the filled in part of the trough.

If marks are present, there is a marks subnode before or after the trough node, below which each mark gets a node with name mark. The marks nodes get either the .top or .bottom style class.

The mark node has a subnode named indicator. If the mark has text, it also has a subnode named label. When the mark is either above or left of the scale, the label subnode is the first when present. Otherwise, the indicator subnode is the first.

The main CSS node gets the 'marks-before' and/or 'marks-after' style classes added depending on what marks are present.

If the scale is displaying the value (see Scale.draw-value), there is subnode with name value. This node will get the .top or .bottom style classes similar to the marks node.

Accessibility

GtkScale uses the AccessibleRole.slider role.

Constructors

new

@classmethod
def new(cls, orientation: Orientation | int, adjustment: Adjustment | None = ...) -> Widget

Creates a new GtkScale.

Parameters:

  • orientation — the scale’s orientation.
  • adjustment — the Adjustment which sets the range of the scale, or None to create a new adjustment.

new_with_range

@classmethod
def new_with_range(cls, orientation: Orientation | int, min: float, max: float, step: float) -> Widget

Creates a new scale widget with a range from min to max.

The returns scale will have the given orientation and will let the user input a number between min and max (including min and max) with the increment step. step must be nonzero; it’s the distance the slider moves when using the arrow keys to adjust the scale value.

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 Scale.set_digits to correct it.

Parameters:

  • orientation — the scale’s orientation.
  • min — minimum value
  • max — maximum value
  • step — step increment (tick size) used with keyboard shortcuts

Methods

add_mark

def add_mark(self, value: float, position: PositionType | int, markup: str | None = ...) -> 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 the marks value.

If markup is not None, text is shown next to the tick mark.

To remove marks from a scale, use Scale.clear_marks.

Parameters:

  • value — the value at which the mark is placed, must be between the lower and upper limits of the scales’ adjustment
  • position — where to draw the mark. For a horizontal scale, PositionType.TOP and PositionType.LEFT are drawn above the scale, anything else below. For a vertical scale, PositionType.LEFT and PositionType.TOP are drawn to the left of the scale, anything else to the right.
  • markup — Text to be shown at the mark, using Pango markup

clear_marks

def clear_marks(self) -> None

Removes any marks that have been added.

get_digits

def get_digits(self) -> int

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

get_draw_value

def get_draw_value(self) -> bool

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

get_has_origin

def get_has_origin(self) -> bool

Returns whether the scale has an origin.

get_layout

def get_layout(self) -> Pango.Layout | None

Gets the PangoLayout used to display the scale.

The returned object is owned by the scale so does not need to be freed by the caller.

get_layout_offsets

def get_layout_offsets(self) -> tuple[int, int]

Obtains the coordinates where the scale will draw the PangoLayout representing the text in the scale.

Remember when using the PangoLayout function you need to convert to and from pixels using PANGO_PIXELS() or PANGO_SCALE.

If the Scale.draw-value property is False, the return values are undefined.

get_value_pos

def get_value_pos(self) -> PositionType

Gets the position in which the current value is displayed.

set_digits

def set_digits(self, digits: int) -> None

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

Also causes the value of the adjustment to be rounded to this number of digits, so the retrieved value matches the displayed one, if Scale.draw-value is True when the value changes. If you want to enforce rounding the value when Scale.draw-value is False, you can set Range.round-digits instead.

Note that rounding to a small number of digits can interfere with the smooth autoscrolling that is built into GtkScale. As an alternative, you can use Scale.set_format_value_func to format the displayed value yourself.

Parameters:

  • digits — the number of decimal places to display, e.g. use 1 to display 1.0, 2 to display 1.00, etc

set_draw_value

def set_draw_value(self, draw_value: bool) -> None

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

Parameters:

  • draw_valueTrue to draw the value

set_format_value_func

def set_format_value_func(self, func: ScaleFormatValueFunc | None = ...) -> None

func allows you to change how the scale value is displayed.

The given function will return an allocated string representing value. That string will then be used to display the scale's value.

If None is passed as func, the value will be displayed on its own, rounded according to the value of the Scale.digits property.

Parameters:

  • func — function that formats the value

set_has_origin

def set_has_origin(self, has_origin: bool) -> None

Sets whether the scale has an origin.

If Scale.has-origin is set to True (the default), the scale will highlight the part of the trough between the origin (bottom or left side) and the current value.

Parameters:

  • has_originTrue if the scale has an origin

set_value_pos

def set_value_pos(self, pos: PositionType | int) -> None

Sets the position in which the current value is displayed.

Parameters:

  • pos — the position in which the current value is displayed

Virtual methods

do_get_layout_offsets

def do_get_layout_offsets(self) -> tuple[int, int]

Obtains the coordinates where the scale will draw the PangoLayout representing the text in the scale.

Remember when using the PangoLayout function you need to convert to and from pixels using PANGO_PIXELS() or PANGO_SCALE.

If the Scale.draw-value property is False, the return values are undefined.

Properties

digits

digits: int  # read/write

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

draw_value

draw_value: bool  # read/write

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

has_origin

has_origin: bool  # read/write

Whether the scale has an origin.

value_pos

value_pos: PositionType | int  # read/write

The position in which the current value is displayed.