Re: [Matplotlib-users] passing mouse clicks back to a polling application
belinda thom wrote: For anyone in my boat following this thread, here's what I ended up doing: Thanks for the summary -- it's really nice to find this sort of thing when scanning archives in the future. I'm sad to hear that its not as easy to use matplotlib to write more sophisticated apps than it is w/matlab Well, there are balances to be struck here. If we wanted MPL to be full featured for GUI stuff -- we have to either pick on GUI toolkit, or essentially write full GUI toolkit -- and there really are enough already! Maybe Matlab's gotten better than it was in my day (version 5), but while simple GUI stuff was doable -- it was really pretty limited and painful. If you want a GUI -- use a GUI toolkit. Once you get over the learning curve, you really will be happier -- so I think John has made the right choice limiting MPL's built-in capabilities. I intend to write a more serious app using Python that needs a GUI (an audio file viewer and editor to be used for my music perception research). It will be well worth it to learn a real GUI toolkit for that kind of thing. BE sure to check out existing projects to that too -- Audacity, transana, etc. I've heard that Apple's own InterfaceBuilder is THE WAY to program GUI-based apps and wonder if anyone has had experience using this w/Python? You can't. If you want Mac-only then you should certainly check out PyObjC -- and use InterfaceBuilder with it. With wx, you can either write the interface code by hand (which I advocate), or use one of a handful of GUI-building tools: Code Blocks wxDesigner wxGlade XRCed PythonCard Dabo (I think they have one now) etc http://www.mithis.com/~chrisb I never found the time to finish my matplotlib-scipy install from source (b/c of the apple's wx incompatibility), but I do intend to finish with that business when I return in June and am wondering if your above rec about a wx latest-version would interfere with that? Nope. I think MPL is working OK with wxPython2.8 now. I hope we'll get a build on pythonmac.org this week. In any case, FloatCanvas works with 2.6 and 2.8. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/ORR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [EMAIL PROTECTED] - 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
Thanks everyone for the advice. For anyone in my boat following this thread, here's what I ended up doing: Had a stroke of insight and coded the whole thing up (poorly) using maptlotlib w/no polling. In other words, the entire API sat in the mouse click callback function. Lame, yes---especially since some expensive AI search can be going on that takes more than a few seconds---but as long as my students take care not to enter more than one mouse click before each screen update, the app doesn't seem to suffer from inconsistency problems. Its pretty darn nice looking w/ matplotlib, too. So, that's the band aid. I'm sad to hear that its not as easy to use matplotlib to write more sophisticated apps than it is w/matlab but am glad that it is documented here. To summarize what I gleaned from Chris, the maptlotlib (interactive?) thread doesn't handle anything but its own stuff (e.g. mouse callbacks) directly, so my attempt to use the threading event/wait stuff failed for that reason (?). The pyrobotics app would be a good candidate for using matplotlib, but might not be easy to use for similar reasons. I am wondering if a socket-based approach, where matplotlib served up, for instance, mouse clicks, plots, etc, to a client app might be a good compromise. I am still unclear how John's recs for timer or idler (which I've been able to find no accessible documentation on) would have helped me. For instance, wouldn't the timer rec fail for the same reason my event wait thread hack did? Being sad, however, I understand that the main focus of matplotlib is high- and scientific-quality graphics, for which it seems to hit the nail on the head. Francisco's rec to the for non-programmers programming a GUI document looks very worthwhile (and I am a seasoned programmer). I intend to have a serious look at it this summer, b/c I intend to write a more serious app using Python that needs a GUI (an audio file viewer and editor to be used for my music perception research). The document does indeed seem like a good introduction to event-based programming, and had I a week or more to spare, would probably have tried using the related packages to build the app. Before the stroke-of-insight, I got lucky and managed to get a simple Tk app working that handled displaying the game and processing mouse clicks, but I foiled in my attempts to capture keyboard input, so after several hours gave up. It does seem wx is more suited for OS X, and my brief googling seems to imply that Tk is becoming the thing people---esp on OS X---USED to use. I've heard that Apple's own InterfaceBuilder is THE WAY to program GUI-based apps and wonder if anyone has had experience using this w/Python? Thanks again for the advice. A last question for Chris... If it does look promising, be sure to get the latest version from: http://www.mithis.com/~chrisb I never found the time to finish my matplotlib-scipy install from source (b/c of the apple's wx incompatibility), but I do intend to finish with that business when I return in June and am wondering if your above rec about a wx latest-version would interfere with that? --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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
On Apr 5, 2007, at 8:41 PM, belinda thom wrote: So, how do the above observations relate to John Hunter's recommendation that I use a timer or idler? It was the reply from him that led me to think I might be able to come up w/something that worked w/o too much dorking. My understanding is that John was recommending a method for writing a GUI application that used matplotlib. You might be able to use that approach to write the whole shebang in pylab, but I suspect it would be difficult to do well. Understood. If I could get something working really quickly, I might go that route. But it would have to take a few hours tops, and that would include me figuring out how to get in mouse clicks and draw graphics. If you need a simple front-end for drawing and receiving mouse clicks, wxPython's FloatCanvas is probably a fine choice. The OSX version of wxPython includes a demo application that showcases the different UI elements it provides. I recommend you play around with the FloatCanvas demo, then look at the documentation and source code to get an idea of how fast you can get up and running with it. Ken - 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
Ken McIvor wrote: I recommend you play around with the FloatCanvas demo, then look at the documentation and source code to get an idea of how fast you can get up and running with it. Thanks for the endorsement, Ken. I will say that while I think FloatCanvas is a good tool for the job, it is going to take more than a few hours unless the user is already experienced with wxPython If it does look promising, be sure to get the latest version from: http://www.mithis.com/~chrisb In addition to a few more features, it includes a bunch of small Demos that show you how to do a variety of things with it. There's a mailing list for questions, too: http://mail.mithis.com/cgi-bin/mailman/listinfo/floatcanvas -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/ORR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [EMAIL PROTECTED] - 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
On Apr 5, 2007, at 6:27 AM, John Hunter wrote: On 4/5/07, belinda thom [EMAIL PROTECTED] 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 - 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
On Apr 5, 2007, at 2:14 PM, belinda thom wrote: 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. You will probably always have this problem when you do things that prevent the GUI's event loop from running. Are you running your code from within iPython or as a script? And then I got Chris's great reply, which makes me wonder if I'm trying to do too much. I'd have to agree with Chris that you are trying to do too much from within pylab. 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. If that's what you're aiming for you'd probably be happier with the result if you write something using Tkinter's Canvas or the wxPython FloatCanvas. In Matlab, I'm used to building applications, so I was hoping it would be possible to do something similar in matplotlib. Well, it's entirely possible to build applications that use matplotlib. The thing to remember is that matplotlib isn't its own programming language or development environment -- it ties Python and and bunch of libraries together to provide a Matlab-like interface in the 'pylab' module. 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). Although I'm matplotlib is going to continue becoming more powerful and flexible, I'd personally be surprised if that ever becomes one of the project's goals. I obviously can't speak for anyone else, but it seems to me that people who want to build GUI applications using matplotlib should be doing so by using matplotlib from within Python. I also suspect that the engineering effort involved in making this functionality happen might be beyond the scope of the project. Ken - 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
On Apr 5, 2007, at 1:20 PM, Ken McIvor wrote: On Apr 5, 2007, at 2:14 PM, belinda thom wrote: 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. You will probably always have this problem when you do things that prevent the GUI's event loop from running. Hmmm. Makes sense. I'll include a tidbit from Chris that I received via personal email b/c it relates: -- Thinking about your issue a bit -- the problem here is that MPL does not provide an event loop. In Matlab, the command line and figure windows share an event loop. In MPL, the event loop is provided by the hosting GUI toolkit, and is separate from command line event loop. For that reason, MPL itself is a bit divorced from the process. That why Ken and I have suggested that you'd be better off embedding MPL in a gui toolkit, if it's just the kind of plotting you need, or using another drawing widget in a GUI toolkit if you really need non- plotting type drawing. If it hadn't been for the bugs, TK probably would have been a fine option for you. - Are you running your code from within iPython or as a script? And then I got Chris's great reply, which makes me wonder if I'm trying to do too much. I'd have to agree with Chris that you are trying to do too much from within pylab. Darn. I am writing this reply as a desperate attempt to make sure there's not some quick fix to make it work in my specific case; I'm about ready to give up or try something like Tk, but am running out of time. We might, alas, have to settle for a command-line based game :-(. So, how do the above observations relate to John Hunter's recommendation that I use a timer or idler? It was the reply from him that led me to think I might be able to come up w/something that worked w/o too much dorking. 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. If that's what you're aiming for you'd probably be happier with the result if you write something using Tkinter's Canvas or the wxPython FloatCanvas. Understood. If I could get something working really quickly, I might go that route. But it would have to take a few hours tops, and that would include me figuring out how to get in mouse clicks and draw graphics. In Matlab, I'm used to building applications, so I was hoping it would be possible to do something similar in matplotlib. Well, it's entirely possible to build applications that use matplotlib. The thing to remember is that matplotlib isn't its own programming language or development environment -- it ties Python and and bunch of libraries together to provide a Matlab-like interface in the 'pylab' module. Fair enough. Thanks for the valuable observation. 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). Although I'm matplotlib is going to continue becoming more powerful and flexible, I'd personally be surprised if that ever becomes one of the project's goals. I obviously can't speak for anyone else, but it seems to me that people who want to build GUI applications using matplotlib should be doing so by using matplotlib from within Python. I also suspect that the engineering effort involved in making this functionality happen might be beyond the scope of the project. Ditto. 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] passing mouse clicks back to a polling application
On 4/5/07, belinda thom [EMAIL PROTECTED] wrote: Darn. I am writing this reply as a desperate attempt to make sure there's not some quick fix to make it work in my specific case; I'm about ready to give up or try something like Tk, but am running out of time. We might, alas, have to settle for a command-line based game :-(. This has already been mentioned, but not in this theread: http://gael-varoquaux.info/computers/traits_tutorial/index.html You may find it useful, he describes a number of concepts that are involved in this discussion in a step-by-step fashion. regards, f - 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.phpp=sourceforgeCID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users