Forum Archive

Custom view class return an object

Webmaster4o

I have a script using UI, and a second which contains a custom view class for modifying an image. At a certain point, script A generates an image. It then passes this image to the custom view class, which is added as a subviews and allows the user to edit the image. I want to have the custom view class return the image when editing is done, back to the main script.

If this doesn't make sense,
1. Script passes an image from script A to an instance of the custom view class from script B
2. User uses custom view class to modify image
3. User confirms that they are finished.
4. Modified image is passed from script B back to script A

I'm having trouble with step 4. This would be accomplished by return from a function, but how from a class?

omz

You could use some sort of callback mechanism for this. For example, let the custom view have a finished_handler attribute that the script sets to a function or method it wants the view to call when it's done editing. The function could have an argument that contains the result of the editing operation.

Remember that functions and methods are basically just objects, so you can not just call them, but also pass them around, set them as attributes, etc.

JonB

+1 for a callback, which can also just be defined inline in Script A, thus keeping the same local scope, etc.

The alternative is to use wait_modal. Look at the source of the dialogs module, for example list_dialog, etc, which do exactly what you describe: present a view, and return something selected by the user.