[vtkusers] ​SetClientData in python (David Gobbi)

Andrew Maclean andrew.amaclean at gmail.com
Wed Nov 15 23:25:45 EST 2017


Hi,

As promised, I did an example, please see:
https://lorensen.github.io/VTKExamples/site/Python/Interaction/CallBack/
and
 https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/CallBack/

Also look at:
https://lorensen.github.io/VTKExamples/site/Python/VisualizationAlgorithms/
StreamlinesWithLineWidget/

Thanks to Bill Lorensen for some solving markdown issues in the case of
CallBack.

Please feel free to improve/correct the examples.

Regards
   Andrew


On Mon, Nov 13, 2017 at 1:26 AM, Oliver Natt <onatt at gmx.de> wrote:

> Dear Andrew,
>
> Davids proposal ist great. However, it appears a bit strange to most
> programmers. I prefer for this purpose to make a new class which is a
> callable. I think, this makes the code more readable for most programmers
> since the data attribute is already initialized within the class itself.
> Your example would look like this:
>
> class DummyCallback:
>     def __init__(self, data):
>         self.data = data
>     def __call__(self, obj, event):
>         print(ev, self.data)
>
> cb = DummyCallback(renwin)
> interactor.AddObserver('LeftButtonPressEvent', cb)
>
> The last two lines can, of course, be abbreviated to:
> interactor.AddObserver('LeftButtonPressEvent', DummyCallback(renwin))
>
> Best regards
> Oliver
>
>
> Am 11.11.2017 um 23:46 schrieb Andrew Maclean:
>
> Ok
> I have got it.
> This works:
>
> def DummyFunc(obj, ev):
>     print(ev, DummyFunc.data)
>
> DummyFunc.data = renwin# How do I pass my obj_2 ?interactor.AddObserver('LeftButtonPressEvent', DummyFunc)
>
>
> ​David, you may be able to explain this better than I can but I assume
> this is what happens:​
>     Here we are adding a new attribute to DummyFunc. We can do this
> because Python functions are in fact objects.
>
> ​This is something totally strange to C++ programmers!
>
> What a neat approach!​
>
>
>
> --
> Oliver Natt,
>
> Jägerstraße 26b
> 90451 Nürnberg
>
> Phone:   +49 (0)  9 11 - 71 58 71 40
> Mobile   +49 (0)  1 76 - 20 82 27 38
> E-Mail:  onatt at gmx.de
>
>


-- 
___________________________________________
Andrew J. P. Maclean

___________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171116/db56315c/attachment.html>


More information about the vtkusers mailing list