[Tutor] Clear screen questions

eryksun eryksun at gmail.com
Mon May 6 09:12:22 CEST 2013


On Sun, May 5, 2013 at 8:54 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On 06/05/13 10:17, boB Stepp wrote:
>>
>> import os
>> ...
>> os.system('cls')

If you want to try your hand at using curses, it lets you clear the
screen and a whole lot more. Christoph Gohlke has a Windows extension
available based on the PDCurses library:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

Here are some curses demos:

http://hg.python.org/cpython/file/026ee0057e2d/Demo/curses

As to clearing the console screen on NT, using the cmd shell's
internal "cls" command is the only easy way I know. The hard way is
using the Win32 console API. Microsoft has demo code here:

http://msdn.microsoft.com/en-us/library/ms682022

Writings spaces to the entire buffer like that is very different from
the way "clear" works. "clear" queries the terminfo database for the
current terminal to get the control sequence used to clear the screen.
In an xterm the following sequences work (note: \E is an escape
character, and [ is the control sequence introducer, or CSI):

    echo -n -e '\E[1;1H'  # move the cursor to the top corner, at
position (1, 1)
    echo -n -e '\E[2J'  # clear the whole screen

Of course you can combine these, and even append a message:

    echo -n -e '\E[1;1H\E[2JSpam!\nSpam!!\nSpam!!1\n'

See the following page for a detailed list of control codes:

http://www.xfree86.org/current/ctlseqs.html

>> Finally, a minor, secondary question: I was curious how my clear
>> screen code would run in the IDLE Python shell window, since I know
>> there is currently no way to clear its screen (other than the blank
>> line approach). Of course it did not clear the screen, but it did do
>> something unexpected that I would like to understand. Upon hitting the
>> clear screen code the active window switched from the Python shell
>> window to the Python edit window. Why is this so?
>
> Magic. IDLE bug. Who knows? It doesn't do that for me, but I've only tested
> it interactively with one version on Linux.

On NT, the IDLE development environment runs via pythonw.exe, which
has no attached console. Thus when system() starts cmd.exe, there's no
console window to inherit from the parent process and a new one has to
be created, and subsequently destroyed when cmd exits. After the
console window was destroyed it's possible that the edit window got
raised to the top instead of the shell window.  To avoid this you can
instead use subprocess.Popen. This lets you start a console process
with a hidden window:

http://docs.python.org/2/library/subprocess#windows-popen-helpers

Also, for pythonw.exe the streams stdin, stdout, and stderr are all
None. As far as I know all of Python's built-in standard I/O functions
such as print() and input() just silently return in this case. But
IDLE is different. Like many other IDEs and GUI shells, it fakes
stdin, stdout, and stderr with proxies. These are connected to its
PyShell and also the subprocess that it uses to actually run your
code. While this is convenient for development and most debugging,
make sure to test your code directly from the OS under 'normal'
conditions.


More information about the Tutor mailing list