ginput in pylab

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
        assert isinstance(n, int), "Requires an integer argument"
        connect('button_press_event', self.on_click)
        self.clicks = []
        tmp = 0
        while len(self.clicks)<n :
            tmp += 1
            if tmp == 100:
        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.