Gtk.Switch
Gtk.Switch — A “light switch” style toggle

Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Switch
Functions:
- new
() -> Gtk.Widget - set_active
(self, is_active:bool) - get_active
(self) -> bool - set_state
(self, state:bool) - get_state
(self) -> bool
Signals:
- “activate”
(widget, user_data)
Description:
Gtk.Switch is a widget that has two states: on or off. The user can control which state should be active by clicking the empty area, or by dragging the handle.
Gtk.Switch can also handle situations where the underlying state changes with a delay. See Gtk.Switch::state-set for details.
Function Details:
new()
new () -> Gtk.Widget
Creates a new Gtk.Switch widget.
Returns: the newly created
Gtk.SwitchinstanceSince: 3.0
set_active()
set_active (self, is_active:bool)
Changes the state of sw to the desired one.
- Since: 3.0
get_active()
get_active (self) -> bool
Gets whether the Gtk.Switch is in its “on” or “off” state.
Returns:
Trueif theGtk.Switchis active, andFALSEotherwiseSince: 3.0
set_state()
set_state (self, state:bool)
Sets the underlying state of the Gtk.Switch.
Normally, this is the same as “active”, unless the switchis set up for delayed state changes. This function is typicallycalled from a “state-set” signal handler.
See “state-set” for details.
- Since: 3.14
get_state()
get_state (self) -> bool
Gets the underlying state of the Gtk.Switch.
Returns: the underlying state
Since: 3.14
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Switch(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.connect("destroy", Gtk.main_quit)
switch = Gtk.Switch()
switch.connect("notify::active", self.on_switch_toggled)
self.add(switch)
def on_switch_toggled(self, switch, state):
if switch.get_active():
print("Switch toggled to on")
else:
print("Switch toggled to off")
window = Switch()
window.show_all()
Gtk.main()