1

Im just wondering if it is possible to have the last thing printed on the top line for example:

Print("hello")
Print("hello, again...")

And have the idle shell look like:

hello, again...
hello

Instead of the other way around. Does anyone know of any command in python that i can use to make the last printed item appear at the top of the shell?

finlx
  • 89
  • 9
  • possible duplicate of [how to clear the screen in python](http://stackoverflow.com/questions/4810537/how-to-clear-the-screen-in-python) – Chris Montanaro Sep 15 '15 at 19:32
  • @ChrisMontanaro it sounds like more than just clearing the screen, they want `curses` that works in IDLE. – Mark Ransom Sep 15 '15 at 19:33

3 Answers3

0

If i had a timer that updates a variable each second, how would i make sure the old value is cleared:

import time
print("hello", end="\r")
time.sleep(1)
print("hello, again...",end="\r")

You will need to run the code from somewhere that is not idle as it is not a real tty, using clear or cls is also going to fail in idle. You could possibly use the curses lib as mentioned in a comment but it will certainly not be trivial to implement, if you wanted to reverse the output like the lines in your question you could redirect stdout to an io.StringIO object and reverse the lines:

from contextlib import redirect_stdout
from io import StringIO
f = StringIO()
with redirect_stdout(f):
    print("hello")
    print("hello, again...")
f.seek(0)
print("".join(f.readlines()[::-1]))

Which in idle will output:

hello, again...
hello

If I were you I would ditch idle, what you are seeing is one of many limitations you may encounter when using idle. If you really do want to stay using idle, you should download idlex which has some extensions to make idle work more like a terminal

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

This worked for me on Windows using Python 3

pip install windows-curses

Example:

import curses
import time

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()

try:           
    stdscr.addstr(0, 0, "Printing Numbers...")
    for i in range(10):
        stdscr.addstr(1, 0, "Number: {0}".format(i))
        stdscr.insertln()
        stdscr.refresh()
        time.sleep(0.5)

finally:
    curses.echo()
    curses.nocbreak()
    curses.endwin()

Using stdscr.insertln() allows you to insert a blank line under the cursor. All following lines are moved down by one line.

More information on curses found here: https://docs.python.org/2/library/curses.html

Haze
  • 171
  • 3
-1

The only thing I an think of is keeping track of everything printed and clearing the console every time.

import os

console_lines = []
def reverse_print(text):
    console_lines.append(text)
    os.system('cls')
    print('\n'.join(reversed(console_lines)))

reverse_print('hi')
reverse_print('hello')
reverse_print('okay')
Cody Bouche
  • 945
  • 5
  • 10
  • How would i go about just clearing the text each time instead of reversing it? If i had a timer that updates a variable each second, how would i make sure the old value is cleared? – finlx Sep 15 '15 at 19:21