交替开关 ON/OFF?

Alternating Switches ON/OFF?

所以我想让我的小 Python Gtk window 有 2 个开关。当一个开关打开时,另一个开关关闭,反之亦然。我不太确定如何控制这两个开关。如果有人能引导我朝着正确的方向前进,将不胜感激。

#!/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()

你可以实现和我写的类似的逻辑:

#!/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()

我会 bind the properties 两个开关,在创建时反转和同步:

#!/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()

解决方案在于这行代码:

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

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

不需要像预先列出的答案那样复杂的东西。 gtk 已经有一个单选按钮小部件可以为您完成这一切。唯一的问题是初始化时没有设置按钮,所以你必须选择一个来设置。