repeat until keypressed

Peter Otten __peter__ at web.de
Mon May 29 10:15:48 EDT 2017


Poul Riis wrote:

> In good old pascal there was this one-liner command:
> repeat until keypressed
> 
> Apparently there is no built-in analogue for that in python. I have
> explored several different possibilities (pyglet, keyboard, curses, ginput
> (from matplotlib) and others) but not managed to find anything that works
> the way I want.
> 
> In the following example I just want to replace 'waitforbuttonpress' with
> something like 'continueuntilbuttonpress' if such a command exists. It
> could be  a mouseclick or a keystroke from the terminal, for instance
> 'shift', 'space' or some character.

How about combining waitforbuttonpress() with pause()?

import warnings
warnings.filterwarnings("ignore", ".*GUI is implemented.*")

from pylab import *

def plot_them():
    plot1 = subplot(2, 1, 1)
    plot2 = subplot(2, 1, 2)

    for i in range(20):
        plot1.plot([20*(sin(i/10)+1)], [cos(i/10)], 'bo')
        if waitforbuttonpress(timeout=0.1):
            return
    for i in range(20):
        plot2.plot([20*(sin(i/10)+1)], [-cos(i/10)], 'ro')
        if waitforbuttonpress(timeout=0.1):
            return

ion()
plot_them()
waitforbuttonpress()
ioff()





More information about the Python-list mailing list