For those who have never used matlab, ginput is a blocking call that
takes one optional argument n, waits for n click on the current figure,
and returns the coordinates of those n clicks. I have been trying to
write such a function in pylab and I can't find a solution.

Here is a first attempt:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
from pylab import *
from time import sleep

class gInput(object):
    """ Class that create a callable object to retrieve mouse click in a
        blocking way, à la MatLab.
    """


    def on_click(self, event):
        """ Event handler that will be passed to the current figure to
            retrive clicks.
        """
        print "called"
        if event.inaxes:
            self.clicks.append((event.x, event.y))
        print self.clicks

    def __call__(self, n):
        """ Blocking call to retrieve n coordinate pairs through mouse
            clicks.
        """
        assert isinstance(n, int), "Requires an integer argument"
        connect('button_press_event', self.on_click)
        self.clicks = []
        tmp = 0
        while len(self.clicks)<n :
            sleep(0.1)
            tmp += 1
            if tmp == 100:
                break
        return self.clicks

ginput = gInput()
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I run this in ipython -pylab.
This fails. I am not to sure why. It seems the "connect" does not happen
until the __call__ function returns. This is probably due to
eventloop/thread problems that I don't master terribly well. Is there a
solution for this problem (ie a blocking call to retrieve coordinates).
If so it would be great to have such a function in pylab.

Cheers,

Gaël

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to