Skip to content

Gtk.LevelBar

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

Shows a level indicator.

Typical use cases are displaying the strength of a password, or showing the charge level of a battery.

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

Use LevelBar.set_value to set the current value, and LevelBar.add_offset_value to set the value offsets at which the bar will be considered in a different state. GTK will add a few offsets by default on the level bar: LEVEL_BAR_OFFSET_LOW, LEVEL_BAR_OFFSET_HIGH and LEVEL_BAR_OFFSET_FULL, with values 0.25, 0.75 and 1.0 respectively.

Note that it is your responsibility to update preexisting offsets when changing the minimum or maximum value. GTK will simply clamp them to the new range.

Adding a custom offset on the bar

static GtkWidget *
create_level_bar (void)
{
  GtkWidget *widget;
  GtkLevelBar *bar;

  widget = gtk_level_bar_new ();
  bar = GTK_LEVEL_BAR (widget);

  // This changes the value of the default low offset

  gtk_level_bar_add_offset_value (bar,
                                  GTK_LEVEL_BAR_OFFSET_LOW,
                                  0.10);

  // This adds a new offset to the bar; the application will
  // be able to change its color CSS like this:
  //
  // levelbar block.my-offset {
  //   background-color: magenta;
  //   border-style: solid;
  //   border-color: black;
  //   border-width: 1px;
  // }

  gtk_level_bar_add_offset_value (bar, "my-offset", 0.60);

  return widget;
}

The default interval of values is between zero and one, but it’s possible to modify the interval using LevelBar.set_min_value and LevelBar.set_max_value. The value will be always drawn in proportion to the admissible interval, i.e. a value of 15 with a specified interval between 10 and 20 is equivalent to a value of 0.5 with an interval between 0 and 1. When LevelBarMode.DISCRETE is used, the bar level is rendered as a finite number of separated blocks instead of a single one. The number of blocks that will be rendered is equal to the number of units specified by the admissible interval.

For instance, to build a bar rendered with five blocks, it’s sufficient to set the minimum value to 0 and the maximum value to 5 after changing the indicator mode to discrete.

GtkLevelBar as GtkBuildable

The GtkLevelBar implementation of the GtkBuildable interface supports a custom <offsets> element, which can contain any number of <offset> elements, each of which must have "name" and "value" attributes.

CSS nodes

levelbar[.discrete]
╰── trough
    ├── block.filled.level-name
    ├── block.empty

GtkLevelBar has a main CSS node with name levelbar and one of the style classes .discrete or .continuous and a subnode with name trough. Below the trough node are a number of nodes with name block and style class .filled or .empty. In continuous mode, there is exactly one node of each, in discrete mode, the number of filled and unfilled nodes corresponds to blocks that are drawn. The block.filled nodes also get a style class .level-name corresponding to the level for the current value.

In horizontal orientation, the nodes are always arranged from left to right, regardless of text direction.

Accessibility

GtkLevelBar uses the AccessibleRole.meter role.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new GtkLevelBar.

new_for_interval

@classmethod
def new_for_interval(cls, min_value: float, max_value: float) -> Widget

Creates a new GtkLevelBar for the specified interval.

Parameters:

  • min_value — a positive value
  • max_value — a positive value

Methods

add_offset_value

def add_offset_value(self, name: str, value: float) -> None

Adds a new offset marker on self at the position specified by value.

When the bar value is in the interval topped by value (or between value and LevelBar.max-value in case the offset is the last one on the bar) a style class named level-``name will be applied when rendering the level bar fill.

If another offset marker named name exists, its value will be replaced by value.

Parameters:

  • name — the name of the new offset
  • value — the value for the new offset

get_inverted

def get_inverted(self) -> bool

Returns whether the levelbar is inverted.

get_max_value

def get_max_value(self) -> float

Returns the max-value of the GtkLevelBar.

get_min_value

def get_min_value(self) -> float

Returns the min-value of the GtkLevelBar.

get_mode

def get_mode(self) -> LevelBarMode

Returns the mode of the GtkLevelBar.

get_offset_value

def get_offset_value(self, name: str | None = ...) -> tuple[bool, float]

Fetches the value specified for the offset marker name in self.

Parameters:

  • name — the name of an offset in the bar

get_value

def get_value(self) -> float

Returns the value of the GtkLevelBar.

remove_offset_value

def remove_offset_value(self, name: str | None = ...) -> None

Removes an offset marker from a GtkLevelBar.

The marker must have been previously added with LevelBar.add_offset_value.

Parameters:

  • name — the name of an offset in the bar

set_inverted

def set_inverted(self, inverted: bool) -> None

Sets whether the GtkLevelBar is inverted.

Parameters:

  • invertedTrue to invert the level bar

set_max_value

def set_max_value(self, value: float) -> None

Sets the max-value of the GtkLevelBar.

You probably want to update preexisting level offsets after calling this function.

Parameters:

  • value — a positive value

set_min_value

def set_min_value(self, value: float) -> None

Sets the min-value of the GtkLevelBar.

You probably want to update preexisting level offsets after calling this function.

Parameters:

  • value — a positive value

set_mode

def set_mode(self, mode: LevelBarMode | int) -> None

Sets the mode of the GtkLevelBar.

Parameters:

  • mode — a GtkLevelBarMode

set_value

def set_value(self, value: float) -> None

Sets the value of the GtkLevelBar.

Parameters:

Properties

inverted

inverted: bool  # read/write

Whether the GtkLeveBar is inverted.

Level bars normally grow from top to bottom or left to right. Inverted level bars grow in the opposite direction.

max_value

max_value: float  # read/write

Determines the maximum value of the interval that can be displayed by the bar.

min_value

min_value: float  # read/write

Determines the minimum value of the interval that can be displayed by the bar.

mode

mode: LevelBarMode | int  # read/write

Determines the way GtkLevelBar interprets the value properties to draw the level fill area.

Specifically, when the value is LevelBarMode.CONTINUOUS, GtkLevelBar will draw a single block representing the current value in that area; when the value is LevelBarMode.DISCRETE, the widget will draw a succession of separate blocks filling the draw area, with the number of blocks being equal to the units separating the integral roundings of LevelBar.min-value and LevelBar.max-value.

value

value: float  # read/write

Determines the currently filled value of the level bar.

Signals

offset-changed

def on_offset_changed(self, name: str) -> None: ...

Emitted when an offset specified on the bar changes value.

This typically is the result of a LevelBar.add_offset_value call.

The signal supports detailed connections; you can connect to the detailed signal "changed::x" in order to only receive callbacks when the value of offset "x" changes.