From: belinda t. <bt...@cs...> - 2007-04-05 19:14:50
|
On Apr 5, 2007, at 6:27 AM, John Hunter wrote: > On 4/5/07, belinda thom <bt...@cs...> wrote: > >> 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 >> > > You need to get out of the mold of thinking about while loops with > blocking input. Instead make everything event driven and track state > variables to decide which player's mouse events to process. So > instead of getting a blocking input, simply do nothing on events > unless you are in a certain state. You can use a timer or an idle > handler for recurring processing rather than a while loop. This was the kind of advice I was looking for. Being so new to Python, I hadn't a clue what to look for. I do think its valuable to be able to write a blocking mouse function. After your pointer to Timer (which led me to the threading Python library) and idle handler (which I didn't find useful doc on, so didn't persue), I came up with the code I'll append below. My printing of time elapsed seems to imply the thing is working as I'd expect (I see times that differ by about 1 second). Problem is, I still get the twirling wheel of deadness on my Mac that led me to post my original message. And then I got Chris's great reply, which makes me wonder if I'm trying to do too much. I've been using pyrorobotics, which relies heavily on Tk, but their plotting facilities are not great. So I started using matplotlib and really like it. And I ran into problems w/their Tk interface where windows wouldn't update correctly on the mouse unless the mouse was clicked in the window. Found some stuff via google implying this was some nasty bug on Mac, didn't find an easy work around, so now try to use matplotlib whenever possible, which explains my current path to trying to use it to provide a simple graphic interface to a python Connect 4 game that I wrote so my students can have fun writing "smart" game players in my AI course. In Matlab, I'm used to building applications, so I was hoping it would be possible to do something similar in matplotlib. Perhaps matplotlib is not currently set up for such things (in which case I'd like to ask if this is something you'd like to include in the future). Thanks again, --b class Mouse : def __init__(self,f,cb) : self.data = None self.cb = cb def getClick(event) : self.data = event.xdata self.cb() f.canvas.mpl_connect("button_press_event",getClick) def blockMouse(f,rng) : import threading import time startTime = time.time() e = threading.Event() def cb() : e.set() m = Mouse(f,cb) def valid(val,rng) : print "time elapsed is %g" % (time.time()-startTime) if val == None : return False for i in rng : if i-.4 <= val <= i+.4 : return True return False # poll til valid while True : e.wait(1) if valid(m.data,rng) : break else: e.clear() m.data = None return m.data def app() : import pylab pylab.close('all') f = pylab.figure() rng = [1,2,3] pylab.plot([1,2,3],[1,2,3]) pylab.axis([0,4,0,4]) while True : mouse = blockMouse(f, rng) if mouse == 2 : break else : print mouse |