A simple way to print few line stuck to the same position

Hans Mulder hansmu at xs4all.nl
Fri Jun 3 16:57:47 EDT 2011


On 3/06/11 13:00:22, TheSaint wrote:

> I'd like to show 4~6 line of report and refreshing periodically all of them,
> avoiding to scroll down.
> example:
>
> this count 50
> Second time 90
> following line 110
> another line xxx
>
> The lines should remain on their position and update their data.

The quick and dirty way would be to output VT100 sequences: they work
on most, if not all modern terminals and emulators.
For example, to move the cursor to the start of line 20 and clear the
rest of the screen:

     sys.stdout.write("\033[20;0H\033[J")

Or to clear the first six lines and put the cursor in the top left:

     for i in range(1, 7):
         sys.stdout.write("\033[%d;0H\033[K" % i)
     sys.stdout.write("\033[0;0H")

After doing that, you could print your report.

A minimalist solution would be to print the labels ("This count", etc.)
only once, and position the cursor after it to update the report.

> I think, I should go for course module, but it's a bit of learning, which I
> didn't embarked yet.

The module is called "curses" and, yes, it would be the best way to go.


-- HansM





More information about the Python-list mailing list