Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Custom view class return an object
-
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,
- Script passes an image from script A to an instance of the custom view class from script B
- User uses custom view class to modify image
- User confirms that they are finished.
- 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? -
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.
-
+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 thedialogs
module, for example list_dialog, etc, which do exactly what you describe: present a view, and return something selected by the user.