[Tutor] How to hide cursor in Terminal?

brandon w bbbgggwww at gmail.com
Fri Feb 17 22:28:46 CET 2012


This is what I ended up using:

import time
import sys
import os

def countd():

    seconds = 59
    minutes = 4
    five_minutes = 0

    os.system('clear')
    os.system('setterm -cursor off')

    while five_minutes != 300:
        sys.stdout.write("\r%d:%02.f\t" % (minutes, seconds))
        sys.stdout.flush()
        seconds -= 1
        if seconds == -1:
            minutes -= 1
            seconds = 59

        five_minutes += 1
        time.sleep(1)

countd()

os.system('setterm -cursor on')


This worked like a charm!
Thank you guys for your help!
Now I'll have to see if I can get the same thing working on a Windows
machine.

Brandon

On Fri, Feb 17, 2012 at 2:00 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 17/02/12 11:38, brandon w wrote:
>
>> I made a timer that counts down from five minutes. This code runs fine
>> but I a seeing a cursor blinking on the first number as the code is
>> running. How do I avoid this?
>>
>>
> Try putting the carriage return at the start of the line. You print the
> line then reset the cursor to the beginning. You want to reset the cursor
> then print leaving the cursor at the end of the line(I assume?)
> If you want to get rid of the cursor entirely then I think you might be
> able to do it via a set tty command or similar - but why would you?!
> And if you do, don't forget to reset it after you finish!
>
>
>      while five_minutes != 300:
>>         sys.stdout.write("%d:%02.f\r" % (minutes, seconds))
>>
>
>          sys.stdout.write("\r%d:%02.f" % (minutes, seconds))
>
> That might do it for you.
>
> --
> 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<http://mail.python.org/mailman/listinfo/tutor>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120217/8dd3cc22/attachment.html>


More information about the Tutor mailing list