# Scale (Python)

Marta Maria Casetti
mmcasetti@gmail.com 2012
A slider widget for selecting a value from a range

# Scale

Slide the scales!

# Code used to generate this example

# Useful methods for a Scale widget

A Gtk.Adjustment is needed to construct the Gtk.Scale. This is the representation of a value with a lower and upper bound, together with step and page increments, and a page size, and it is constructed as `Gtk.Adjustment(value, lower, upper, step_increment, page_increment, page_size)` where the fields are of type `float`; `step_increment` is the increment/decrement that is obtained by using the cursor keys, `page_increment` the one that is obtained clicking on the scale itself. Note that `page_size` is not used in this case, it should be set to `0`.

In line 28 the signal `"value-changed"` is connected to the callback function `scale_moved()` using `widget.connect(signal, callback function)`. See for a more detailed explanation.

`get_value()` retrieves the current value of the scale; `set_value(value)` sets it (if the `value`, of type `float`, is outside the minimum or maximum range, it will be clamped to fit inside them). These are methods of the class Gtk.Range.

Use `set_draw_value(False)` to avoid displaying the current value as a string next to the slider.

To highlight the part of the scale between the origin and the current value:

self.h_scale.set_restrict_to_fill_level(False) self.h_scale.set_fill_level(self.h_scale.get_value()) self.h_scale.set_show_fill_level(True)

in the callback function of the "value-changed" signal, so to have the new filling every time the value is changed. These are methods of the class Gtk.Range.

`add_mark(value, position, markup)` adds a mark at the `value` (`float` or `int` if that is the precision of the scale), in `position` (`Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT, Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM`) with text `Null` or `markup` in the Pango Markup Language. To clear marks, `clear_marks()`.

`set_digits(digits)` sets the precision of the scale at `digits` digits.

# API References

In this sample we used the following:

GtkScale

GtkAdjustment

Standard Enumerations