Re: Gtk.Switch.set_state() without setting active




If the service didn't start I would think that the switch should be off and inactive. I put a little test code together in Python to look at the different states and you should be able to set the on/off and background the way you need them.

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

class MainWindow(Gtk.Window):
    def __init__ (self):
        Gtk.Window.__init__(self, title="Delayed Switch")
        self.set_size_request (200, 100)
        self.set_position(Gtk.WindowPosition.CENTER)

        self.switch1 = Gtk.Switch()
        self.switch1.set_hexpand(True)
        self.switch1.set_vexpand(True)
        self.switch1.connect("state_set", self.switch_state)
       
        grid = Gtk.Grid()
        grid.attach(self.switch1, 0, 0, 1, 1)
        self.add(grid)

    def switch_state(self, switch, state):
        if(state):
            self.switch1.set_sensitive(False)
            GObject.timeout_add(1000, self.start_process)
            return True
        else:
            return False

    def start_process(self):
        num = random.random()
        if(num < 0.2):
            self.switch1.set_sensitive(True)
            self.switch1.set_state(False)
            self.switch1.set_active(False)
            print("Service didn't start! Number " + str(num) + " Return False")
            return False
        else:
            print("Number " + str(num) + " Return True")
            return True
       
window = MainWindow()
window.connect("delete_event", Gtk.main_quit)
window.show_all()
Gtk.main()



-----Original Message-----
From: segfault <segfault riseup net>
To: gtk-list <gtk-list gnome org>
Sent: Thu, Nov 3, 2016 5:13 am
Subject: Gtk.Switch.set_state() without setting active

Hi,

I'm using a Gtk.Switch to start/stop a service. I want to use the
"state" property to indicate whether the service is actually running,
and the "active" property to indicate whether the user wants the service
to be running. I got the "delayed state change" described here [1]
working, i.e. when the user turns the switch from off to on, the "state"
is not set until the service is actually running. But then if I use
set_state(False), e.g. because the service ran into an error and exited,
the "active" property is set to False too, which is not what I want. Is
there any way to set "state" without setting "active"?

I hope it's clear what I want to achieve. If not, I can prepare some
screenshots to explain it.

[1]
https://developer.gnome.org/gtk3/stable/GtkSwitch.html#GtkSwitch-state-set

Cheers
_______________________________________________
gtk-list mailing list
gtk-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]