473,378 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Printing to console, no scroll

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


Jul 18 '05 #1
8 11709
"Totte Karlsson" <mt*@qm.com> writes:
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()
for second in range(10):
time.sleep(1)
msg(`10-second` +" seconds ") #add a space at the end to delimit
print #to finish off the line

--
ha************@boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601
Jul 18 '05 #2
>

"Totte Karlsson" <mt*@qm.com> writes:
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 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 <jp@demonseed.net>
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

Jul 18 '05 #3
Sorry for my post with all the ansi.py code.

I forgot the website I got it from.

Allen
Jul 18 '05 #4
Harry George <ha************@boeing.com> wrote in message news:<xq*************@cola2.ca.boeing.com>...
"Totte Karlsson" <mt*@qm.com> writes:
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()
for second in range(10):
time.sleep(1)
msg(`10-second` +" seconds ") #add a space at the end to delimit
print #to finish off the line


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.
Jul 18 '05 #5
PiedmontBiz wrote:

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

I forgot the website I got it from.


Google is your friend (along with five seconds of effort):

http://www.demonseed.net/~jp/code/ansi.py

-Peter
Jul 18 '05 #6
>From: Peter Hansen pe***@engcorp.com
Date: 1/15/04 11:42 AM Eastern Standard Time
Message-id: <40***************@engcorp.com>

PiedmontBiz wrote:

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

I forgot the website I got it from.


Google is your friend (along with five seconds of effort):

http://www.demonseed.net/~jp/code/ansi.py

-Peter



Appreciate the rebuke. Also, the url was saved in the html by IExplorer. I
could have looked there.

Allen
Jul 18 '05 #7
"Totte Karlsson" <mt*@qm.com> wrote:

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)

--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #8
> 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.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Totte Karlsson | last post by:
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...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
0
by: Alex Moskalyuk | last post by:
I am using System.Windows.Forms.TextBox to publish some real-time data that's coming off the external devices. My data is fed into the TextBox with proper precautions not to exceed the MaxSize...
2
by: PHead | last post by:
Is there any way to stop the console from scrolling? What I mean isnt to not scroll at all, its that I want to get rid of the buffer. When a new line is created, I want row 0 to go away...
4
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a...
1
by: kasargee | last post by:
In IE printing if scroll bar is moved to middle position and printing is done then print out are coming from scroll bar start position. But i want to print whatever seen on screen ,if scroll bar is...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.