I have a issue where I want a Gtk.Switch to start and stop a function. This function should work as long as the switch is active. For example it could just print "Function is on" as long as switch is active.
But unless I thread this function it will freeze GUI and it won't be possible to stop it.
### Gtk Gui ###
self.sw = Gtk.Switch()
self.sw.connect("notify::active",
self.on_sw_activated)
### Gtk Gui ###
### Function ###
def on_sw_activated(self, switch, gparam):
if switch.get_active():
state = "on"
else:
state = "off"
### This needs to be "threaded" as to not freeze GUI
while state == "on":
print("Function is on")
time.sleep(2)
else:
print("Function is off")
### Function ###
As far as I know there is no good way to stop a thread in python, my question is if there is another way of implementing this without using python threads.