Alternating Switches ON/OFF?

delay off timer circuit
5 second timer circuit
swagatam circuit
swagatam electronic
swagatam innovations inverter
home circuit projects

So I want to have my small Python Gtk window have 2 switches. When one switch is ON, the other is turned OFF, and vice versa. I am not too sure how to control both switches. If anyone can lead me in the right direction, it'd be much appreciated.

#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class SwitcherWindow(Gtk.Window):

def __init__(self):
    Gtk.Window.__init__(self, title="Alt Switch Demo")
    self.set_border_width(10)

    hbox = Gtk.Box(spacing=10)
    self.add(hbox)

    switch1 = Gtk.Switch()
    switch1.connect("notify::active", self.on_switch_activated)
    switch1.set_active(True)
    hbox.pack_start(switch1, True, True, 0)

    switch2 = Gtk.Switch()
    switch2.connect("notify::active", self.on_switch_activated)
    switch2.set_active(False)
    hbox.pack_start(switch2, True, True, 0)

    if switch1.get_active():
        switch2.set_active(False)
    else:
        switch2.set_active(True)

def on_switch_activated(self, switch, gparam):
    builder = Gtk.Builder()
    sw1 = builder.get_object("switch1")
    sw2 = builder.get_object("switch2")
    if switch.get_active():
        state = "on"
        sw2.set_active(False)
    else:
        state = "off"
        print("Switch was turned", state)

win = SwitcherWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

I would bind the properties of both switches, inverted and sync'ed on creation:

#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject 

class SwitcherWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Alt Switch Demo")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=10)
        self.add(hbox)

        switch1 = Gtk.Switch()
        switch1.set_active(True)
        hbox.pack_start(switch1, True, True, 0)

        switch2 = Gtk.Switch()
        switch2.set_active(False)
        hbox.pack_start(switch2, True, True, 0)

        switch1.bind_property("active", switch2, "active", GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE |  GObject.BindingFlags.INVERT_BOOLEAN)   


win = SwitcherWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

The solution resides on this line of code:

switch1.bind_property("active", switch2, "active", GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE |  GObject.BindingFlags.INVERT_BOOLEAN) 

Here you bind the "active" property of switch1 and switch2 with the binding flags: bidirectional, sync on create and invert boolean

Alternating On/Off Switch for Lamps and Light-Emitting Diodes , Alternating On/Off Switch for Lamps and Light-Emitting DiodesAs we already know, LEDs Duration: 1:23 Posted: Nov 30, 2010 Alternating ON-OFF Switch Use this circuit instead of a standard on-off switch. Switching is very gentle. Connect unused input pins to an appropriate logic level (I used ground). IC1, the MC14069 (or 4069) is a regular Hex-inverter type and is constructed with MOS P-channel and N-channel enhancement mode devices in a single monolithic structure. …

You can implement a similar logic to what I wrote:

#!/usr/bin/env python


class Switch:
    _State = False

    def __init__(self, StartingPosition=False):
        self._State = StartingPosition

    def SwitchON(self):
        self._State = True

    def SwitchOFF(self):
        self._State = False

    def Switch(self):
        self._State = not self._State

    def GetInfo(self):
        print(self._State)


class Switcher:
    def __init__(self, switchDependencyList=[]):
        self.SwitchDependencyList = switchDependencyList
        if len(self.SwitchDependencyList) == 0:
            return None
        if not len(self.SwitchDependencyList) % 2:
            return None

    def SwitchByIndex(self, Index):
        for i, switch in enumerate(self.SwitchDependencyList):
            if i == Index:
                switch.SwitchON()
            else:
                switch.SwitchOFF()

    def GetInfo(self):
        for switch in self.SwitchDependencyList:
            switch.GetInfo()


sw1 = Switch()
sw2 = Switch()
SwitcherModule = Switcher([sw1, sw2])
SwitcherModule.SwitchByIndex(1)
SwitcherModule.GetInfo()

Adafruit On-Off-On-Off Alternating Power Button / Pushbutton 3-Way , It's basically a push-button toggle switch that connects and disconnects the middle pin from either of the two side switches with every click. This clicky switch​  Use this circuit instead of a standard on-off switch. Switching is very gentle. If you don't use the Printed Circuit Board, connect unused input pins to an appropriate logic level ('+' or '-'). Unused output pins *MUST* be left open! On the pcb this is already done. First 'push' activates the relay, another 'push' de-activates the relay.

No need for anything as complex as the prelisted answers. gtk already has a radiobutton widget that does it all for you. Only thing is that when it is initialised you have no buttons set, so you have to pick one to set.

Switching Two Alternate Loads ON/OFF with IC 555, But alternate switching of these driers manually always is tiresome. So I request a circuit which will be able to switch on and switch off the the  alternating On-Off Switch. Use this circuit instead of a standard on-off switch. Switching is very gentle. Connect unused input pins to an appropriate logic level (I

[PDF] On-Off-On-Off Alternating Power Button / Pushbutton 3-Way , last push and its off again! It's basically a push-button toggle switch that connects and disconnects the middle pin from either of the two side switches with every  The circuit is built around a standard IC 555 astable configuration, which basically produces an alternating high and low or an alternating 12V and zero volt at its pin#3 when switched ON. This alternately switching output accompanies certain delay between its ON/OFF switching as determined by the component values of R1, R2, and C.

US4647742A - Alternate on-off switch mechanism, An alternate mechanism comprising a plunger (4) supported for a linear movement, a rotary cam (71) rotatably supported by the plunger, a spring (43) for​  Get the circuit instead of a standard on-off switch. Switching is very gentle. If we don’t use the PCB, connect unused input pins to an appropriate logic level (‘+’ or ‘-’).

Turn relay on/off with pulse. Pulse-on, Pulse-off, that meets what I need to do (except for an alternating relay which is. reason why you can't just use an alternate action mechanical switch? Alternating ON-OFF Switch #2 using transistors, electronic building kit

Comments
  • Hi Paul, the question specifically asks for GtkSwitch'es to be used. Binding two properties is nothing one could characterize as "complex", if you compare the question code with the solution one you would see that it has less code. I think that your answer could be a comment and IF it was a route to follow, then you could present a solution. Cheers
  • No it does not ask specifically for a GtkSwitch. It just asks for a switch. A GtkSwitch is given by example. The functionality the OP asks for is a radio button and for UX it is better to present a widget that visually demonstrates that functionality than widgets that don't. None of the answers above also tinker around on the ATK side of things. It is important to establish relationships between items for accessibility.
  • Hi again Paul, I think you are assuming too much without questioning @Mauricio Rivera. You could be right but your "answer" is not an answer it's a comment. If you want to present it as an answer then care to elaborate a real answer. That was just my intention. Good luck
  • The OP asked open endedly to be led in the right direction. Seriously. How is it not a "real" answer.