stuck after a mpl_connect - how to disconnect and go on

Hi

sorry to post this again but all my attempts to solve the matplotlib
problem below failed and I desperately need this to progress.

I would like to use mpl_connect and disconnect to examine a series of 2d
arrays in turn (with a "for" loop), one after the other:

==> at each iteration I'd like to be able to use the left mouse button
to evaluate the sum of all x,y coordinates I select by (right) clicking
somewhere in the present array, and then switch to the next 2d data
array after I hit the right mouse button (button==3). I have no clue how
to do this and the program I wrote so far is just hanging there and does
nothing. Didn't see anything like this in the archive.

Any way to get out of this? Thanks for your help.

Eric

···

#=================
# stupid simple code just to illustrate the kind of things I would like to achieve
# This does not work and just hang there...
#=================
import numpy as num

class offset:
   def __init__(self) :
      self.xc = 0.
      self.yc = 0.
      self.stay = 1
   def on_click(self, event) :
      if event.button == 1:
         if event.inaxes is not None:
            self.xc += event.xdata
            self.yc += event.ydata
      elif event.button == 3:
         self.stay = 0

data1 = num.random.rand(10,10)
data2 = num.random.rand(10,10)
data3 = num.random.rand(10,10)
alldata = [data1, data2, data3]
for i in range(len(alldata)) :
   imshow(alldata[i])
   newoffset = offset()
   binding = connect('button_press_event', newoffset.on_click)
   while newoffset.stay :
      pass

   disconnect(binding)
   print newoffset.xc, newoffset.yc

Hi Eric,

Hi

sorry to post this again but all my attempts to solve the matplotlib
problem below failed and I desperately need this to progress.

I would like to use mpl_connect and disconnect to examine a series of 2d
arrays in turn (with a "for" loop), one after the other:

==> at each iteration I'd like to be able to use the left mouse button
to evaluate the sum of all x,y coordinates I select by (right) clicking
somewhere in the present array, and then switch to the next 2d data
array after I hit the right mouse button (button==3). I have no clue how
to do this and the program I wrote so far is just hanging there and does
nothing. Didn't see anything like this in the archive.

Any way to get out of this? Thanks for your help.

As a general rule of thumb, if you're using a loop like while
newoffset.stay: pass, then you're doing something wrong. This will
take control of all execution and let nothing else happen. You have to
do the control by switching between functions. Here's a modification
to your code that I think does what you want:

import numpy as n
import pylab as p

class offset:
    def __init__(self, parent) :
        self.parent = parent
        self.xc = 0.
        self.yc = 0.
        self.stay = 1

    def on_click(self, event) :
        if event.button == 1:
            if event.inaxes is not None:
                print "Adding point %d, %d" % (event.xdata, event.ydata)
                self.xc += event.xdata
                self.yc += event.ydata
        elif event.button == 3:
            print "Switching data"
            self.parent.show_next()

class displayer:
    def __init__(self):
        self.i = 0
        self.alldata = [n.random.rand(10,10) for x in xrange(3)]
        self.newoffset = offset(self)
        self.binding = p.connect('button_press_event', self.newoffset.on_click)
        self.show_next()

    def show_next(self):
        if self.i < len(self.alldata):
            p.imshow(self.alldata[self.i])
            self.i += 1
        else:
            p.disconnect(self.binding)
            print self.newoffset.xc, self.newoffset.yc

I hope that helps you see the concept.

Angus.

···

On 23/08/07, Eric Emsellem <emsellem@...419...> wrote:
--
AJC McMorland, PhD Student
Physiology, University of Auckland

Angus is right w/ resepct to mpl -- we don't have any support for
blocking calls. This is a very frequent request and you are certainly
right Eric to *expect* something like this to work, but in the context
of multiple GUIs w/ threads it is not too easy. One could probably
make tkagg work with only minor modifications to mpl, but the other
GUIs would be harder becasue they are threaded. So using event
handling and callbacks are the way to solve this problem, as Angus
suggests in his solution, but they are harder for people to get their
head around than blocking calls, so hopefully we will get support for
this idiom before the end of time.

JDH

···

On 8/22/07, Angus McMorland <amcmorl@...287...> wrote:

> I would like to use mpl_connect and disconnect to examine a series of 2d
> arrays in turn (with a "for" loop), one after the other:
>
> ==> at each iteration I'd like to be able to use the left mouse button
> to evaluate the sum of all x,y coordinates I select by (right) clicking
> somewhere in the present array, and then switch to the next 2d data
> array after I hit the right mouse button (button==3). I have no clue how
> to do this and the program I wrote so far is just hanging there and does
> nothing. Didn't see anything like this in the archive.
>
> Any way to get out of this? Thanks for your help.

As a general rule of thumb, if you're using a loop like while
newoffset.stay: pass, then you're doing something wrong.