473,379 Members | 2,646 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,379 software developers and data experts.

Re printing on same line.

I want to print a count down timer on the same line. I tried

print '\r', timeLeft,
which just appends to the same line.
thanx,

jh

Jun 15 '07 #1
5 5077
En Fri, 15 Jun 2007 12:21:01 -0300, HMS Surprise <jo**@datavoiceint.com>
escribió:
I want to print a count down timer on the same line. I tried

print '\r', timeLeft,
which just appends to the same line.
It works OK for me. Don't you have any other print statement ruining the
output?

--
Gabriel Genellina

Jun 15 '07 #2
>... ruining the
output?
? None.

Jun 15 '07 #3
On 6/15/07, HMS Surprise <jo**@datavoiceint.comwrote:
I want to print a count down timer on the same line. I tried

print '\r', timeLeft,

which just appends to the same line.
Sounds to me like whatever you're printing to doesn't do what you
expect when it encounters a carriage return (\r). Is your program
running in a terminal? Both the windows cmd.exe shell and bash under
linux seem to do the right thing when encountering a '\r'.

On the other hand, running the same code in IDLE doesn't work right,
presumably because the text window doesn't really know what to do with
a bare carriage return.

--
Jerry
Jun 15 '07 #4
Jerry Hill wrote:
On 6/15/07, HMS Surprise <jo**@datavoiceint.comwrote:
>I want to print a count down timer on the same line. I tried

print '\r', timeLeft,

which just appends to the same line.

Sounds to me like whatever you're printing to doesn't do what you
expect when it encounters a carriage return (\r). Is your program
running in a terminal? Both the windows cmd.exe shell and bash under
linux seem to do the right thing when encountering a '\r'.
Actually, bash has nothing to do with how the terminal handles \r. The job
of the shell (bash, ksh, csh, sh ...) is to execute your script when you
type its name.

Outputting a \r might or might not move the cursor to the beginning of the
line. It's completely system specific, and even on the same OS, it depends
on the capabilities of the actual terminal the programs run on, and on some
terminal emulators it might depend on configuration settings.

If you need to explore the capabilities of the terminal, curses will be a
good place to start.

--
rbh
Jun 15 '07 #5
Robert Bauck Hamar <ro**********@ifi.uio.nowrote:
Jerry Hill wrote:
>On 6/15/07, HMS Surprise <jo**@datavoiceint.comwrote:
>>I want to print a count down timer on the same line. I tried
>> print '\r', timeLeft,
>>which just appends to the same line.
>Sounds to me like whatever you're printing to doesn't do what you
expect when it encounters a carriage return (\r). Is your program
running in a terminal? Both the windows cmd.exe shell and bash under
linux seem to do the right thing when encountering a '\r'.
Actually, bash has nothing to do with how the terminal handles \r. The job
of the shell (bash, ksh, csh, sh ...) is to execute your script when you
type its name.
Outputting a \r might or might not move the cursor to the beginning of the
line. It's completely system specific, and even on the same OS, it depends
on the capabilities of the actual terminal the programs run on, and on some
terminal emulators it might depend on configuration settings.
If you need to explore the capabilities of the terminal, curses will be a
good place to start.
--
rbh
Sometimes you don't want to full curses mode switch and other baggage.
You can use the 'tput' utility to get the terminal escape sequences
for various cursor movements for your terminal. Read the tput man
page for some information, and the terminfo(5) man page for more.

In particular you can do things like (from a shell prompt):

tput sc; tput cup 0 30;echo Front and Center; tput rc

... which saves the current cursor location (sc), executes a cursor
position to the first line, 30th column (cup 0 30), writes some text
there, and then restores the cursor location (rc).

The trick, in Python, is that you can read these sequences into
your program via popen() calls and then print them wherever you
want to use them. You can even set colors (foreground and background),
and text attributes (dim, bright, blinking, underline) and perform
various other curses like applications without actually doing the
curses mode changes if you're really a bit masochistic.

However, for the original poster's purposes the easiest option would
probably be to print something like:

print chr(0x08) * 80, "new line stuff" + " " * 66

(Print lots of backspaces followed by whatever information you wanted to
over-write the line with and padding with enough spaces to clear any
other stuff you wanted to over-write).

Yes, this is sloppy. Yes, it might not work on some terminals or under
some terminal settings. However, it should work under most circumstances
on most terminals --- including some which wouldn't support the curses
module. You can improve it somewhat by keeping track of how many
characters you've printed to the current (last) line and dynamically
printing the precise number of backspaces and padding spaces that are
needed.

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

Jun 20 '07 #6

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

Similar topics

9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
3
by: Grim Reaper | last post by:
I print mailing labels out of Access 2000 databases about 3 to 4 times a week. I have been having problems with one thing since I have been printing mailing labels. I print mailing labels by...
5
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using...
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...
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
4
by: Lucas Ponzo | last post by:
Hi All, I have an ASP.NET 2.0 app. The users access the pages, uniquely via pocket pc ... I need to print a page. But I need that the page print on a printer installed on the web server...
2
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default...
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...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.