def toggle_tree_view_listening(tree_view: Gtk.TreeView, handler_id: int, func: Callable) -> Optional[int]: if handler_id is not None: # The handler exists, stop listening tree_view.disconnect(handler_id) return None else: # The handler doesn't exist, start listening return tree_view.connect('cursor-changed', func)