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/OR&R(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.php&p=sourceforge&CID=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.php&p=sourceforge&CID=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/OR&R(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.php&p=sourceforge&CID=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.php&p=sourceforge&CID=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
Christopher Barker ha scritto: Matplotlib is a plotting library -- it's not a gui development lib. I'd think about using a tool designed for the job. I don't know what you want the graphics of your game to look like, but depending on that, some suggestions: PyGame -- good for fancy raster graphics, not so good if you have a need for other GUI elements -- all the typical controls, text boxes etc. TkInter -- it has a Canvas people like a lot that gives a lot of flexibility. wxPython -- The wxPython FloatCanvas (full disclosure -- it's my baby) could probably help you out a lot here, if you're dealing with vector graphics, and particularly if you want zooming and panning. I'm sure you could do it with pyGTK or pyQT also. Deciding between all these options is hard, they all have their strengths. What's best for you is a function of what the needs of your app are, what your future needs may be, what platforms you need to support, and taste. Even if MPL is a perfect fit for your graphics, if you get beyond a basic "calculate and plot" app, you'll probably want to use MPL embedded in a GUI toolkit, TK, GTK or wx. I fully endorse what it's said here. I only want to add: Don't let yourself confused by the fact that most MPL documentation makes references to Pylab. This is a really confusing aspect that I've already pointed out in this ml. MPL is a fully featured Python plotting library. It is conceived (also) to be used inside a fully featured GUI app, and addons like wxMPL make the merging quite painless. Pylab is a Pythonic interactive environment that makes use of MPL and it's good for people that already have confidence with python AND want an interactive environment for quick scripts and interaction à là Matlab. But MPL is *much more* than Pylab. m. -- Massimo Sandal University of Bologna Department of Biochemistry "G.Moruzzi" snail mail: Via Irnerio 48, 40126 Bologna, Italy email: [EMAIL PROTECTED] tel: +39-051-2094388 fax: +39-051-2094387 begin:vcard fn:Massimo Sandal n:Sandal;Massimo org:University of Bologna;Department of Biochemistry adr:;;Via Irnerio 48;Bologna;;40126;Italy email;internet:[EMAIL PROTECTED] tel;work:+39-051-2094388 tel;fax:+39-051-2094387 x-mozilla-html:FALSE version:2.1 end:vcard - 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
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.php&p=sourceforge&CID=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.php&p=sourceforge&CID=DEVDEV ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net
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.php&p=sourceforge&CID=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.php&p=sourceforge&CID=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
Belinda, While I'm sure you can find a way to do what you want with MPL, it sounds like you're really pushing the bounds of what it's designed for. Having been a Matlab user myself, I can see how you've gotten here, but I think you should consider other options. Matplotlib is a plotting library -- it's not a gui development lib. I'd think about using a tool designed for the job. I don't know what you want the graphics of your game to look like, but depending on that, some suggestions: PyGame -- good for fancy raster graphics, not so good if you have a need for other GUI elements -- all the typical controls, text boxes etc. TkInter -- it has a Canvas people like a lot that gives a lot of flexibility. wxPython -- The wxPython FloatCanvas (full disclosure -- it's my baby) could probably help you out a lot here, if you're dealing with vector graphics, and particularly if you want zooming and panning. I'm sure you could do it with pyGTK or pyQT also. Deciding between all these options is hard, they all have their strengths. What's best for you is a function of what the needs of your app are, what your future needs may be, what platforms you need to support, and taste. Even if MPL is a perfect fit for your graphics, if you get beyond a basic "calculate and plot" app, you'll probably want to use MPL embedded in a GUI toolkit, TK, GTK or wx. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R(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.php&p=sourceforge&CID=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: > 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. JDH - 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
Re: [Matplotlib-users] passing mouse clicks back to a polling application
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
Re: [Matplotlib-users] passing mouse clicks back to a polling application
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
[Matplotlib-users] passing mouse clicks back to a polling application
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 - 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