Hi Angus and the Matplotlib community,

I'm finally getting around to trying your suggestions, which at first  
blush seem like they'd do what I want, so THANKS for that!

But I'm still running into issues for which I'm uncertain how to  
proceed. I outline my stumbling blocks below, which have to do with  
what I'd like my app to do. No doubt my understanding of writing GUI- 
style code in Python/matplotlib is lacking, but it seems my app needs  
more structure than the typical usage of mouse clicks in matplotlib.

I outline my thinking below.

On Mar 23, 2007, at 6:11 PM, Angus McMorland wrote:

> ---------------------------
> 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,

My app needs to look something like this:

------

create a game and display it in a matplotlib figure

while game not over :

    if its player 1's turn, get player 1's valid mouse input,  
otherwise get player 2's

    once we've got a valid mouse click, update game to make the  
corresponding move for the current player's turn

    update the figure to display this change

    check if someone's won or game's a draw, only upon which do we  
break out of the while

after having a result, report the answer, do some book keeping, and  
then return from the app

------

I've not been able to figure out how to unwind this app into  
something that only needs to progress when there's been a valid mouse  
click. If I understand your suggestion correctly, it seems the entire  
app should be the callback, but the processing of the clicks by the  
app is complicated.

I wonder if you mean I should massage the app so that it IS the  
callback, but its got enough state that to figure out how to execute  
the appropriate next portion of code seems like it would be a big  
mess (e.g. I'd have to write some kind of FSM whose transitions were  
mouse clicks). I might be able to make this work, but its gonna be  
ugly. It also breaks some of the OO I'd been using in other settings.

What I think I want is a way to get blocking valid mouse input. I had  
thought the Python yield statement might help me (for instance, when  
I'm supposed to get the next mouse click, I call yield and then the  
callback would somehow wake the application back up), but it is  
described in Python as being used only with generator functions, so I  
don't think that does the trick.

I'm fairly new to Python, and even more so to maptlotlib, but I've  
used matlab plenty. There, there's a ginput function that returns a  
value only when the mouse's been clicked. Obviously there's some dual- 
threading going on, but I've never had to write it myself.

I've spent several hours searching on the internet and haven't come  
up with much. One scheme that might work would be to use sockets to  
control the passing of info. But that seems overkill.

A big part of my problem from the matplotlib end is the only info  
I've been able to find on using mouse clicks are basically not app- 
based, e.g. http://www.scipy.org/Cookbook/Matplotlib/ 
Interactive_Plotting. There, the callbacks simply redraw the screen  
by labeling data or whatnot, but there's no sophisticated "main"  
program running at the same time.

Any ideas?

Thx,

--b





-------------------------------------------------------------------------
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

Reply via email to