[Tutor] Primitive Chess Clock Program Question
wolfrage8765 at gmail.com
wolfrage8765 at gmail.com
Tue Jan 10 14:38:23 CET 2012
>> I assume you want to display something like this:
>>
>> Enter your next move: 0:30
SNIP
> Assuming Steven has guessed right then I think you need to use one of the
> non blocking input mechanisms like kbhit() or getch() or somesuch.
>
> Those methods are notioriously unreliable and OS specific. For example you
> may need to use curses or the Microsoft runtime module msvcrt.
I would recommend termios for unix over curses, because curses pretty
much takes over. Something like this:
http://code.activestate.com/recipes/577977-get-single-keypress/
SNIP
> label while awaiting user input is almost trivial
> (for a GUI).
I agree with Alan, a GUI toolkit makes this trivial because it can run
using events and timers. For a terminal I would recommend you use a
couple of threads. One that checks for input on getchar() and the
other thread, the main thread, that can update the display of the
prompt. You can use Queue to pass the key(s) recieved to the main
thread. Also to reduce or eliminate flicker I would also recommend you
look at sending the backspace characters to the terminal and then
replace the correct time. Something like this:
import sys
sys.stdout.write('\b')
sys.stdout.flush()
Of course that is assuming that your terminal will support \b .
On Wed, Jan 4, 2012 at 2:22 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 03/01/12 21:28, Steven D'Aprano wrote:
>
>> I assume you want to display something like this:
>>
>> Enter your next move: 0:30
>>
>> where the "0:30" is the time remaining, and is constantly updating. When
>> it hits zero, the function returns whether the user has typed anything
>> or not.
>
>
> Assuming Steven has guessed right then I think you need to use one of the
> non blocking input mechanisms like kbhit() or getch() or somesuch.
>
> Those methods are notioriously unreliable and OS specific. For example you
> may need to use curses or the Microsoft runtime module msvcrt.
>
> The general code will look like
>
> Display prompt
> while no key hit
> sleep briefly
> update time in prompt
> (using ctrl characters to delete/overwrire previouis entry)
> #when key hit
> process input.
>
> It is a non trivial problem and the details will depend on whether you use
> curses or the microsoft route.
>
> It is one of those few cases that is actually much easier to do
> in a GUI. GUIs generally make life more complex but in this case updating a
> label while awaiting user input is almost trivial
> (for a GUI).
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list