On 24/03/07, belinda thom <[EMAIL PROTECTED]> wrote: > Hi, > > I'm trying to write a very simple GUI using matplotlib and have > gotten stuck. Hopefully someone out there has done something similar > and can point me on my way. > > First, I should mention that the examples provided with matplotlib > weren't immediately helpful to me, because when I try to run various > demos (like pick_event_demo or object_picker) it fails b/c I'm > relying on (and have to rely on) TkAgg. Sadly, I'm too new to > understand what I would need to do to get those demos working. So I > found someone processing mouseclicks using a Mac online and started > there. > > I ended up with something like this: > > from pylab import * > class gui : > def __init__(self) : > self.f = figure() > self.data = None # valid mouse click hasn't yet happened > def clicker(event): > self.data = event.xdata > self.f.canvas.mpl_connect("button_press_event",clicker) > > def getNextValidClick(self) : > (data, self.data) = (None, None) > while True : > print "Waiting for valid mouse click..." > while self.data == None : > pass # keep polling > if 1 <= self.data <= 3 : > # consider this a valid next mouse click > (data, self.data) = (self.data, None) > break > return data > > With which I tried: > > g = gui() > x = g.getNextValidClick() > > but the latter line caused me to experience the spinning wheel of > dead that we mac users so enjoy. > > I have the feeling I need to explicitly yield or some such in the > poll loop, but I don't know how to do that. > > Advice greatly appreciated, both on the code I've provided, and on if > there is a better way altogether to provide an app with data obtained > via a matplotlib mouse click callback. > > Thanks, > > --b
It's the polling that's the problem. Why not use a slightly different approach, like this: --------------------------- from pylab import figure class gui : def __init__(self, callback) : self.f = figure() self.ax = self.f.add_subplot(111) self.ax.plot([1,2,3]) self.data = None # valid mouse click hasn't yet happened def clicker(event): self.data = event.xdata if 1 <= self.data <= 3: callback(self.data) self.f.canvas.mpl_connect("button_press_event",clicker) print "Waiting for valid mouse click..." ---------------------------- then in your application (or shell): def my_cb(inp): # processing here print inp g = gui(my_cb) No polling required, and you only get the valid clicks calling your routine. I hope that helps, A. -- AJC McMorland, PhD Student Physiology, University of Auckland ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users