Adw.ToggleGroup

g Adw.ToggleGroup Adw.ToggleGroup GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget Gtk.Orientable Gtk.Orientable GObject.GInterface->Gtk.Orientable GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.Orientable->Adw.ToggleGroup Gtk.Widget->Adw.ToggleGroup

Subclasses:

None

Methods

Inherited:

Gtk.Widget (183), GObject.Object (37), Gtk.Accessible (17), Gtk.Buildable (1), Gtk.Orientable (2)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

add (toggle)

get_active ()

get_active_name ()

get_can_shrink ()

get_homogeneous ()

get_n_toggles ()

get_toggle (index)

get_toggle_by_name (name)

get_toggles ()

remove (toggle)

remove_all ()

set_active (active)

set_active_name (name)

set_can_shrink (can_shrink)

set_homogeneous (homogeneous)

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9)

Properties

Inherited:

Gtk.Widget (35), Gtk.Accessible (1), Gtk.Orientable (1)

Name

Type

Flags

Short Description

active

int

r/w/en

active-name

str

r/w/en

can-shrink

bool

r/w/en

homogeneous

bool

r/w/en

n-toggles

int

r

toggles

Gtk.SelectionModel

r

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1)

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1)

Class Details

class Adw.ToggleGroup(**kwargs)
Bases:

Gtk.Widget, Gtk.Orientable

Abstract:

No

Structure:

Adw.ToggleGroupClass

A group of exclusive toggles.

<picture> <source srcset=”toggle-group-dark.png” media=”(prefers-color-scheme: dark)”> <img src=”toggle-group.png” alt=”toggle-group”> </picture>

AdwToggleGroup presents a set of exclusive toggles, represented as [class`Toggle`] objects. Each toggle can display an icon, a label, an icon and a label, or a custom child.

Toggles are indexed by their position, with the first toggle being equivalent to 0, and so on. Use the [property`ToggleGroup`:py:data::active<Adw.ToggleGroup.props.active>] to get that position.

Toggles can also have optional names, set via the [property`Toggle`:py:data::name<Adw.ToggleGroup.props.name>] property. The name of the active toggle can be accessed via the [property`ToggleGroup`:py:data::active-name<Adw.ToggleGroup.props.active_name>] property.

AdwToggle objects can be retrieved via their index or name, using [method`ToggleGroup`.get_toggle] or [method`ToggleGroup`.get_toggle_by_name] respectively. AdwToggleGroup also provides a [iface`Gtk`.SelectionModel] of its toggles via the [property`ToggleGroup`:py:data::toggles<Adw.ToggleGroup.props.toggles>] property.

AdwToggleGroup is orientable, and the toggles can be displayed horizontally or vertically. This is mostly useful for icon-only toggles.

Use the [property`ToggleGroup`:py:data::homogeneous<Adw.ToggleGroup.props.homogeneous>] property to make the toggles take the same size, and the [property`ToggleGroup`:py:data::can-shrink<Adw.ToggleGroup.props.can_shrink>] to control whether the toggles can ellipsize.

Example of an AdwToggleGroup UI definition:

``xml
<object class=”AdwToggleGroup”>

<property name=”active-name”>picture</property> <child>

<object class=”AdwToggle”>

<property name=”icon-name”>camera-photo-symbolic</property> <property name=”tooltip” translatable=”yes”>Picture Mode</property> <property name=”name”>picture</property>

</object>

</child> <child>

<object class=”AdwToggle”>

<property name=”icon-name”>camera-video-symbolic</property> <property name=”tooltip” translatable=”yes”>Recording Mode</property> <property name=”name”>recording</property>

</object>

</child>

</object>

``

See also: [class`InlineViewSwitcher`].

CSS nodes

AdwToggleGroup has a main CSS node with the name toggle-group.

Its toggles have CSS nodes with the name toggle, and its separators have nodes with the name separator.

Toggle nodes will have a different style classes depending on their content: .text-button for labels, .image-button for icons, .image-text-button for both or no style class for custom children.

The hidden separators use the .hidden style class.

Style classes

AdwToggleGroup can use the .flat style class to remove its background and make it look like a group of buttons.

<picture> <source srcset=”toggle-group-flat-dark.png” media=”(prefers-color-scheme: dark)”> <img src=”toggle-group-flat.png” alt=”toggle-group-flat”> </picture>

It can also use the .round style class to make its toggles and the group itself rounded.

<picture> <source srcset=”toggle-group-round-dark.png” media=”(prefers-color-scheme: dark)”> <img src=”toggle-group-round.png” alt=”toggle-group-round”> </picture>

They can also be combined with each other.

<picture> <source srcset=”toggle-group-flat-round-dark.png” media=”(prefers-color-scheme: dark)”> <img src=”toggle-group-flat-round.png” alt=”toggle-group-flat-round”> </picture>

Accessibility

AdwToggleGroup uses the GTK_ACCESSIBLE_ROLE_RADIO_GROUP role. Its toggles use the GTK_ACCESSIBLE_ROLE_RADIO role.

New in version 1.7.

classmethod new()
Returns:

the newly created AdwToggleGroup

Return type:

Gtk.Widget

Creates a new AdwToggleGroup.

New in version 1.7.

add(toggle)
Parameters:

toggle (Adw.Toggle) – the toggle to add

Adds a toggle to self.

New in version 1.7.

get_active()
Returns:

the active toggle index

Return type:

int

Gets the index of the active toggle in self.

Returns GTK_INVALID_LIST_POSITION if no toggle is active.

New in version 1.7.

get_active_name()
Returns:

the active toggle name

Return type:

str or None

Gets the name of the active toggle in self.

Can be NULL if the currently active toggle doesn’t have a name.

See [property`Toggle`:py:data::name<Adw.ToggleGroup.props.name>].

New in version 1.7.

get_can_shrink()
Returns:

whether the toggles can shrink

Return type:

bool

Gets whether the toggles can be smaller than the natural size of their contents.

New in version 1.7.

get_homogeneous()
Returns:

whether all toggles take the same size

Return type:

bool

Gets whether all toggles take the same size.

New in version 1.7.

get_n_toggles()
Returns:

the number of toggles

Return type:

int

Gets the number of toggles within self.

New in version 1.7.

get_toggle(index)
Parameters:

index (int) – toggle’s index

Returns:

the toggle

Return type:

Adw.Toggle or None

Gets the toggle with index from self.

New in version 1.7.

get_toggle_by_name(name)
Parameters:

name (str) – toggle name

Returns:

the toggle

Return type:

Adw.Toggle or None

Gets the toggle with the name name from self.

New in version 1.7.

get_toggles()
Returns:

a GtkSelectionModel for the group’s toggles

Return type:

Gtk.SelectionModel

Returns a [iface`Gio`.ListModel] that contains the toggles of the group.

This can be used to keep an up-to-date view. The model also implements [iface`Gtk`.SelectionModel] and can be used to track and change the active toggle.

remove(toggle)
Parameters:

toggle (Adw.Toggle) – a toggle to remove

Removes toggle from self.

New in version 1.7.

remove_all()

Removes all toggles from self.

New in version 1.7.

set_active(active)
Parameters:

active (int) – toggle index

Sets the active toggle for self.

If the index is larger than the number of toggles in self, unsets the current active toggle.

New in version 1.7.

set_active_name(name)
Parameters:

name (str or None) – toggle name

Sets the active toggle for self.

The name can be set via [property`Toggle`:py:data::name<Adw.ToggleGroup.props.name>].

If name is NULL, unset the current active toggle instead.

New in version 1.7.

set_can_shrink(can_shrink)
Parameters:

can_shrink (bool) – whether the toggles can shrink

Sets whether the toggles can be smaller than the natural size of their contents.

If can_shrink is TRUE, the toggle labels will ellipsize.

See [property`Gtk`.Button:can-shrink].

New in version 1.7.

set_homogeneous(homogeneous)
Parameters:

homogeneous (bool) – whether all toggles should take the same size

Sets whether all toggles take the same size.

New in version 1.7.

Property Details

Adw.ToggleGroup.props.active
Name:

active

Type:

int

Default Value:

4294967295

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The index of the active toggle.

Setting the index to a larger value than the number of toggles in the group unsets the current active toggle.

If no toggle is active, the property will be set to [const`Gtk`.INVALID_LIST_POSITION].

New in version 1.7.

Adw.ToggleGroup.props.active_name
Name:

active-name

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The name of the active toggle.

The name can be set via [property`Toggle`:py:data::name<Adw.ToggleGroup.props.name>]. If the currently active toggle doesn’t have a name, the property will be set to NULL.

Set it to NULL to unset the current active toggle.

New in version 1.7.

Adw.ToggleGroup.props.can_shrink
Name:

can-shrink

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the toggles can be smaller than the natural size of their contents.

If set to TRUE, the toggle labels will ellipsize.

See [property`Gtk`.Button:can-shrink].

New in version 1.7.

Adw.ToggleGroup.props.homogeneous
Name:

homogeneous

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether all toggles take the same size.

New in version 1.7.

Adw.ToggleGroup.props.n_toggles
Name:

n-toggles

Type:

int

Default Value:

0

Flags:

READABLE

The number of toggles within the group.

New in version 1.7.

Adw.ToggleGroup.props.toggles
Name:

toggles

Type:

Gtk.SelectionModel

Default Value:

None

Flags:

READABLE

A selection model with the groups’s toggles.

This can be used to keep an up-to-date view. The model also implements [iface`Gtk`.SelectionModel] and can be used to track and change the active toggle.

New in version 1.7.