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




You should be able to setup the behavior you are looking for. Once you have your service started then you might want to check to see if it is running periodically. Maybe the service even has a good return value on exit that has information why it got stopped. For just some general testing to get the idea you can set up another timer to check the state of the started service.

Eric


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.timeout_id=0

        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:
            print("Timout ID " + str(self.timeout_id))
            if(self.timeout_id > 0):
                print("Remove service check timer.")
                GObject.source_remove(self.timeout_id)
                self.timout_id = 0
            return False

    def start_process(self):
        num = random.random()
        if(num < 0.2):
            self.switch1.set_sensitive(True)
            self.switch1.set_state(True)
            print("Service started! Number " + str(num) + " Return False")
            #Keep check that the service is running.
            self.timeout_id = GObject.timeout_add(1000, self.service_killed)
            return False
        else:
            print("Number " + str(num) + " Return True")
            return True

    def service_killed(self):
        print("Check if service is running.")
        num = random.random()
        if(num < 0.15 and self.switch1.get_state()):
            print("Service Killed " + str(num))
            self.switch1.set_state(False)
            self.switch1.set_active(False)
            return False
        else:
            return True
       
window = MainWindow()
window.connect("delete_event", Gtk.main_quit)
window.show_all()
Gtk.main()





-----Original Message-----
From: segfault <segfault riseup net>
To: cecashon <cecashon aol com>; gtk-list <gtk-list gnome org>
Sent: Fri, Nov 4, 2016 4:30 pm
Subject: Re: Gtk.Switch.set_state() without setting active

Hi, I'm not a member of the list, but I read your email in the archive.
I actually sent my email 14 days ago, it seems like it was hold for
moderator approval since.

First, thanks a lot for your work on this! Still, your proposal does not
solve the problem I wanted to solve. I think I didn't explain it
precisely enough: I tried to set the state to False _after_ it was
_already_ set to True (the service runs into an error at some point
_after_ it was started successfully).

I came to the conclusion that this might just not be possible, because
the state is only designed for a delayed state change, but not for a
state change independent from the active state.

Cheers

cecashon aol com:
>
>
>
> I made a mistake and replied to your e-mail directly. New to the GTK list myself. I re-sent to the list and put some test code in Python this time since that is what it looks like you are using. Hopefully things work better now.
>
> Eric
>
>
>
>
> -----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]