question about animation and widgets


I am probably reinventing the wheel. I am trying to get "rolling" graph a la labview with matplotlib. I have more or less managed to get what I want using the simple animation technique provided by matplotlib. I would like now to have a button an the same graph that would enable me to stop the program that is in an infinite acquisition loop.

I have managed to use a matplotlib widget with the graph to make it print something on a terminal, however I do not see how to make it stop an infinite loop. I have tried to make the button change the state of a variable and give the state of this variable a a stop condition to the loop. However this doesn't work. In fact the button doesen't seems to be "listening" during the loop.

Any idea of how I should be doing that ?