Printing to console, no scroll

T

Totte Karlsson

Hi,
How can I print to the console without having it scrolling to a new line for
each print statement?
I want to print a count down in the console, but for each count it scrolls
the screen (of course).

Is there another way?

Here is the simple script for now

print "Closing window in :"
for second in range(10):
time.sleep(1)
print `10-second` +" seconds"

thanks
/totte
 
H

Harry George

Totte Karlsson said:
Hi,
How can I print to the console without having it scrolling to a new line for
each print statement?
I want to print a count down in the console, but for each count it scrolls
the screen (of course).

Is there another way?

Here is the simple script for now

print "Closing window in :"
for second in range(10):
time.sleep(1)
print `10-second` +" seconds"

thanks
/totte

You need to flush after the print statements. If you have several
prints (not just 1 as in this example), it is easier to hide in a
separate function.

def msg(txt):
sys.stdout.write(txt)
sys.stdout.flush()
 
P

PiedmontBiz

=====
Here is some code i got off the web.
I use it on my os2 2.1 laptop for which I don't have curses or tkinker libs.
I mostly use the color functions and move
as a analog to turbo pascal/basic gotoxy.

# begin code

#!/usr/bin/env python
'''
ansi.py

ANSI Terminal Interface

(C)opyright 2000 Jason Petrone <[email protected]>
All Rights Reserved

Color Usage:
print RED + 'this is red' + RESET
print BOLD + GREEN + WHITEBG + 'this is bold green on white' + RESET

Commands:
def move(new_x, new_y): 'Move cursor to new_x, new_y'
def moveUp(lines): 'Move cursor up # of lines'
def moveDown(lines): 'Move cursor down # of lines'
def moveForward(chars): 'Move cursor forward # of chars'
def moveBack(chars): 'Move cursor backward # of chars'
def save(): 'Saves cursor position'
def restore(): 'Restores cursor position'
def clear(): 'Clears screen and homes cursor'
def clrtoeol(): 'Clears screen to end of line'
'''

################################
# C O L O R C O N S T A N T S #
################################
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'

RESET = '\033[0;0m'
BOLD = '\033[1m'
REVERSE = '\033[2m'

BLACKBG = '\033[40m'
REDBG = '\033[41m'
GREENBG = '\033[42m'
YELLOWBG = '\033[43m'
BLUEBG = '\033[44m'
MAGENTABG = '\033[45m'
CYANBG = '\033[46m'
WHITEBG = '\033[47m'

def move(new_x, new_y):
'Move cursor to new_x, new_y'
print '\033[' + str(new_x) + ';' + str(new_y) + 'H'

def moveUp(lines):
'Move cursor up # of lines'
print '\033[' + str(lines) + 'A'

def moveDown(lines):
'Move cursor down # of lines'
print '\033[' + str(lines) + 'B'

def moveForward(chars):
'Move cursor forward # of chars'
print '\033[' + str(lines) + 'C'

def moveBack(chars):
'Move cursor backward # of chars'
print '\033[' + str(lines) + 'D'

def save():
'Saves cursor position'
print '\033[s'

def restore():
'Restores cursor position'
print '\033[u'

def clear():
'Clears screen and homes cursor'
print '\033[2J'

def clrtoeol():
'Clears screen to end of line'
print '\033[K'

# ======end code=======



allen
 
P

PiedmontBiz

Sorry for my post with all the ansi.py code.

I forgot the website I got it from.

Allen
 
M

mensanator

Harry George said:
You need to flush after the print statements. If you have several
prints (not just 1 as in this example), it is easier to hide in a
separate function.

def msg(txt):
sys.stdout.write(txt)
sys.stdout.flush()

That produces

10 seconds 9 seconds 8 seconds 7 seconds 6 seconds 5 seconds 4 seconds
3 seconds 2 seconds 1 seconds



This will produce a true countdown - the messages overwite each other:

for sec in range(10):
time.sleep(1)
m = "%2d seconds" % (10-sec)
msg(m + chr(13))

But this will only work on systems that properly handle control
characters. It works from a Windows command prompt but doesn't from
Idle which shows that silly undefined character box (which I'll
show here as []). So the Idle output looks like

10 seconds[] 9 seconds[] 8 seconds[] 7 seconds[] 6 seconds[]
5 seconds[] 4 seconds[] 3 seconds[] 2 seconds[] 1 seconds[]

which, when copied and pasted into Google, results in

10 seconds
9 seconds
8 seconds
7 seconds
6 seconds
5 seconds
4 seconds
3 seconds
2 seconds
1 seconds

This is the reason why the PROPER way to end a new line is
with a carriage_return + line_feed and not simply line_feed.
It also worked properly from the shell in Cygwin, but I
don't have a real unix or linux system to try it on.
 
T

Tim Roberts

Totte Karlsson said:
How can I print to the console without having it scrolling to a new line for
each print statement?
I want to print a count down in the console, but for each count it scrolls
the screen (of course).

Is there another way?

Here is the simple script for now

print "Closing window in :"
for second in range(10):
time.sleep(1)
print `10-second` +" seconds"

print "Closing window in : "
for second in range(10,0,-1):
print "%2d\x08\x08\x08" % second,
time.sleep(1)
 
W

Will Stuyvesant

I use it on my os2 2.1 laptop for which I don't have curses or tkinker libs.

Do you also know how to get it to work on a Windows XP laptop? I
tried cmd.exe and command.com, but they don't so ansi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top