Gtk.ToggleButton

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Actionable Gtk.Actionable GObject.GInterface->Gtk.Actionable Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget 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.Button Gtk.Button Gtk.Actionable->Gtk.Button Gtk.Buildable->Gtk.Widget Gtk.ToggleButton Gtk.ToggleButton Gtk.Button->Gtk.ToggleButton Gtk.ConstraintTarget->Gtk.Widget Gtk.Widget->Gtk.Button

Example

../_images/ToggleButton.png
Subclasses:

None

Methods

Inherited:

Gtk.Button (16), Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1), Gtk.Actionable (5)

Structs:

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

class

new ()

class

new_with_label (label)

class

new_with_mnemonic (label)

get_active ()

set_active (is_active)

set_group (group)

toggled ()

Virtual Methods

Inherited:

Gtk.Button (2), Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9), Gtk.Actionable (4)

do_toggled ()

Properties

Inherited:

Gtk.Button (6), Gtk.Widget (34), Gtk.Accessible (1), Gtk.Actionable (2)

Name

Type

Flags

Short Description

active

bool

r/w/en

group

Gtk.ToggleButton

w

Signals

Inherited:

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

Name

Short Description

toggled

Emitted whenever the GtkToggleButton’s state is changed.

Fields

Inherited:

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

Name

Type

Access

Description

button

Gtk.Button

r

Class Details

class Gtk.ToggleButton(**kwargs)
Bases:

Gtk.Button

Abstract:

No

Structure:

Gtk.ToggleButtonClass

A GtkToggleButton is a button which remains “pressed-in” when clicked.

Clicking again will cause the toggle button to return to its normal state.

A toggle button is created by calling either [ctor`Gtk`.ToggleButton.new] or [ctor`Gtk`.ToggleButton.new_with_label]. If using the former, it is advisable to pack a widget, (such as a GtkLabel and/or a GtkImage), into the toggle button’s container. (See [class`Gtk`.Button] for more information).

The state of a GtkToggleButton can be set specifically using [method`Gtk`.ToggleButton.set_active], and retrieved using [method`Gtk`.ToggleButton.get_active].

To simply switch the state of a toggle button, use [method`Gtk`.ToggleButton.toggled].

Grouping

Toggle buttons can be grouped together, to form mutually exclusive groups - only one of the buttons can be toggled at a time, and toggling another one will switch the currently toggled one off.

To add a GtkToggleButton to a group, use [method`Gtk`.ToggleButton.set_group].

CSS nodes

GtkToggleButton has a single CSS node with name button. To differentiate it from a plain GtkButton, it gets the .toggle style class.

Accessibility

GtkToggleButton uses the Gtk.AccessibleRole.TOGGLE_BUTTON role.

Creating two Gtk.ToggleButton widgets.

```c static void output_state (Gtk.ToggleButton *source, object user_data) { g_print (“Toggle button “%s” is active: %s”, Gtk.Button.get_label (GTK_BUTTON (source)), Gtk.ToggleButton.get_active (source) ? “Yes” : “No”); }

static void make_toggles (void) { Gtk.Widget *window, *toggle1, *toggle2; Gtk.Widget *box; const str *text;

window = Gtk.Window.new (); box = Gtk.Box.new (Gtk.Orientation.VERTICAL, 12);

text = “Hi, I’m toggle button one”; toggle1 = Gtk.ToggleButton.new_with_label (text);

g_signal_connect (toggle1, “toggled”, G_CALLBACK (output_state), None); Gtk.Box.append (GTK_BOX (box), toggle1);

text = “Hi, I’m toggle button two”; toggle2 = Gtk.ToggleButton.new_with_label (text); g_signal_connect (toggle2, “toggled”, G_CALLBACK (output_state), None); Gtk.Box.append (GTK_BOX (box), toggle2);

Gtk.Window.set_child (GTK_WINDOW (window), box); Gtk.Window.present (GTK_WINDOW (window)); } ```

classmethod new()[source]
Returns:

a new toggle button.

Return type:

Gtk.Widget

Creates a new toggle button.

A widget should be packed into the button, as in [ctor`Gtk`.Button.new].

classmethod new_with_label(label)[source]
Parameters:

label (str) – a string containing the message to be placed in the toggle button.

Returns:

a new toggle button.

Return type:

Gtk.Widget

Creates a new toggle button with a text label.

classmethod new_with_mnemonic(label)[source]
Parameters:

label (str) – the text of the button, with an underscore in front of the mnemonic character

Returns:

a new GtkToggleButton

Return type:

Gtk.Widget

Creates a new GtkToggleButton containing a label.

The label will be created using [ctor`Gtk`.Label.new_with_mnemonic], so underscores in label indicate the mnemonic for the button.

get_active()[source]
Returns:

whether the button is pressed

Return type:

bool

Queries a GtkToggleButton and returns its current state.

Returns True if the toggle button is pressed in and False if it is raised.

set_active(is_active)[source]
Parameters:

is_active (bool) – True or False.

Sets the status of the toggle button.

Set to True if you want the GtkToggleButton to be “pressed in”, and False to raise it.

If the status of the button changes, this action causes the [signal`Gtk`.ToggleButton::toggled] signal to be emitted.

set_group(group)[source]
Parameters:

group (Gtk.ToggleButton or None) – another GtkToggleButton to form a group with

Adds self to the group of group.

In a group of multiple toggle buttons, only one button can be active at a time.

Setting up groups in a cycle leads to undefined behavior.

Note that the same effect can be achieved via the [iface`Gtk`.Actionable] API, by using the same action with parameter type and state type ‘s’ for all buttons in the group, and giving each button its own target value.

toggled()[source]

Emits the ::toggled signal on the GtkToggleButton.

Deprecated since version 4.10: There is no good reason for an application ever to call this function.

do_toggled() virtual

Emits the ::toggled signal on the GtkToggleButton.

Deprecated since version 4.10: There is no good reason for an application ever to call this function.

Signal Details

Gtk.ToggleButton.signals.toggled(toggle_button)
Signal Name:

toggled

Flags:

RUN_FIRST

Parameters:

toggle_button (Gtk.ToggleButton) – The object which received the signal

Emitted whenever the GtkToggleButton’s state is changed.

Property Details

Gtk.ToggleButton.props.active
Name:

active

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

If the toggle button should be pressed in.

Gtk.ToggleButton.props.group
Name:

group

Type:

Gtk.ToggleButton

Default Value:

None

Flags:

WRITABLE

The toggle button whose group this widget belongs to.