repeat until keypressed
Terry Reedy
tjreedy at udel.edu
Mon May 29 17:08:44 EDT 2017
On 5/29/2017 12:14 PM, Dennis Lee Bieber wrote:
> On Mon, 29 May 2017 06:14:46 -0700 (PDT), Poul Riis <priisdk at gmail.com>
> declaimed the following:
>
>> 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.
>>
>
> What OS?
>
>> 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.
>
> I'm presuming this "waitforbuttonpress" is a feature of pylab -- but I
> can't be sure as you used the polluting "import *" (Google seems to
> indicate it is matplotlib)
>
> Under Windows one has access to the msvcrt module
>
> if msvcrt.kbhit(): break
>
> ... But I don't know how this will interact with a graphical window (the
> msvcrt module provides functions for /console/ interaction [kbhit, getch,
> putch, ungetch] and for locking regions of files)
If a tk(inter) window has input focus, the msvcrt console key functions
do not work. For instance, when one runs code via IDLE, IDLE's Shell
gets the focus. I documented this for IDLE under "IDLE-console
differences" after someone reported a problem with kbhit on
Stackoverflow. I presume it is true for other GUIs.
If one has a tkinter GUI, the following might work:
go = True
def key_pressed(event): go = False
<bind key-pressed event to key_pressed>
while go:
calculate()
root.update() # allow events to be processed
The while-loop can be and in some cases will have to be replaced by
root.after calls.
--
Terry Jan Reedy
More information about the Python-list
mailing list