I’m attempting to attach a listener to a window object that is destroyed when the window is closed.
However, the window object does not have an OnClose type method that is called from clicking the X to close (It’s Autodesk Maya’s pymel.core.uitypes.Window if you’re curious why).
add_listener creates a listener that exists in the active session. attach_listener I am creating to cause the listener to remove itself when the window closes, and requires a window to be attached to.
My solution is as follows:
def attach_listener(window, event_name, function):
def wrapper(*args, **kwargs):
if window.exists():
return function(*args, **kwargs)
else:
remove_listener(id_number)
id_number = add_listener(event_name, wrapper)
This obviously won’t work because id_number is not defined until after the function is declared, however, I cannot pass the function to add_listener without defining it first. id_number cannot be passed to add_listener. What do I do to get around this?
You can try this kind of approach: