Search code examples
pythonsignalsinfinite-looppygobject

PyGtk3 ~ Allowing a signal from the user, but not the programmatically


My question is related to PyGObject, Gtk3 and signal suppression.

I have a ComboBoxText, with a signal connected.

self.cbtPaths = Gtk.ComboBoxText()
self.cbtPaths.connect("changed", self.on_changed_path)

def on_changed_path(self,action):
    path = self.cbtPaths.get_active_text()
    i = self.hPaths.change(path)
    self.cbtPaths.set_active(i) #1

self.on_changed_path() is called when the user changes the ComboBoxText. Which is good.

on_changed_path() is called when the code carries out the line marked #1. This causes a never ending loop.

My question is: How do I prevent the line at #1 from triggering the changed signal.?

Note: I call self.cbtPaths.set_active(i) in another part of the code too.


Solution

  • You probably need handler_block() and handler_unblock(). The former blocks a handler of an instance to not be called during any signal emission. That handler will be blocked until you unblock it.

    You can try something like:

    self.cbtPaths.handler_block(self.on_changed_path)
    self.cbtPaths.set_active(i)
    self.cbtPaths.handler_unblock(self.on_changed_path)
    

    You can also check GOBject API's documentation, although is a bit limited.