Discussion:
IDLE: clearing the screen
(too old to reply)
Cave Man
2024-06-04 13:34:17 UTC
Permalink
Hello everyone,

I am new to Python, and I have been using IDLE (v3.10.11) to run small
Python code. However, I have seen that the output scrolls to the bottom
in the output window.

Is there a way to clear the output window (something like cls in command
prompt or clear in terminal), so that output stays at the top?


Thanks in anticipation!
Stefan Ram
2024-06-04 14:19:01 UTC
Permalink
Post by Cave Man
I am new to Python, and I have been using IDLE (v3.10.11) to run small
Python code. However, I have seen that the output scrolls to the bottom
in the output window.
Is there a way to clear the output window (something like cls in command
prompt or clear in terminal), so that output stays at the top?
No, unless you patch IDLE to add such a feature!

(When I wrote [4] here recently, I had thought of just such cases!)

But you can implement a console yourself using tkinter.

As a motivating example see [1]. But as a beginner you may
not be able to modify [1] for your needs unless you learn
some tkinter first, for that I recommend [2] and [3].

[1]

import time
import tkinter as tk

class TextWrapper:
def __init__(self, master):
self.master = master
self.text = tk.Text(master)
self.text.pack()

def print( self, line: str )-> None:
"""Append a line to the Text component."""
self.text.insert( tk.END, line + "\n" )
self.text.see( tk.END ) # Scroll to the end

def reset( self )->None:
"""Clear the Text component."""
self.text.delete( 1.0, tk.END )

def put( line: str )-> None:
text_wrapper.print( line ); root.update(); time.sleep( 1 )

def cls()-> None:
text_wrapper.reset(); root.update(); time.sleep( 1 )

def execute_after_one_second():
put( "Hello, World!" )
put( "This is a test." )
put( "Another line." )

cls()

put( "Text has been reset." )

root = tk.Tk()
text_wrapper = TextWrapper(root)
root.after( 1000, execute_after_one_second )
root.mainloop()

[2]

the GUI part of "Programming Python" by Mark Lutz

[3]

"Python Tkinter" or "Modern Tkinter for Busy Python
Developers" by Mark Roseman
(He has a good tkinter tutorial on his web site)

[4]

We need somethin like a portable curses module (plus colorama)
and it has got to work on both Windoze and Linux straight out
of the box in standard Python. And it would be even sicker if it
could run in that IDLE console too! That way, we could code up
some legit terminal-based software (like slrn or Nethack) using
just plain ol' Python, no extra stuff required. It's a real
shame it ain't already part of the standard library. Someone's
got to step up and make this happen. Python needs a portable
console TUI!
Rob Cliffe
2024-06-04 21:43:14 UTC
Permalink
Welcome to Python!  A great language for program development.

Answers might be platform-dependent (are you using WIndows, Linux, etc.).
However, the following works for me on WIndows.  You can put it in the
startup.py file so you don't have to type it every time you start up the
IDLE.

import os
def cls(): x=os.system("cls")

Now whenever you type
cls()
it will clear the screen and show the prompt at the top of the screen.

(The reason for the "x=" is: os.system returns a result, in this case
0.  When you evaluate an expression in the IDE, the IDE prints the
result.  So without the "x=" you get an extra line at the top of the
screen containing "0".)

I am sure that some jiggery-pokery could be used so you don't have to
type the "()".  But that's more advanced ...

Best wishes
Rob Cliffe
Post by Cave Man
Hello everyone,
I am  new to Python, and I have been using IDLE (v3.10.11) to run
small Python code. However, I have seen that the output scrolls to the
bottom in the output window.
Is there a way to clear the output window (something like cls in
command prompt or clear in terminal), so that output stays at the top?
Thanks in anticipation!
Cameron Simpson
2024-06-05 03:09:29 UTC
Permalink
Post by Rob Cliffe
import os
def cls(): x=os.system("cls")
Now whenever you type
cls()
it will clear the screen and show the prompt at the top of the screen.
(The reason for the "x=" is: os.system returns a result, in this case
0.  When you evaluate an expression in the IDE, the IDE prints the
result.  So without the "x=" you get an extra line at the top of the
screen containing "0".)
Not if it's in a function, because the IDLE prints the result if it
isn't None, and your function returns None. So:

def cls():
os.system("cls")

should be just fine.
Rob Cliffe
2024-06-05 22:16:13 UTC
Permalink
Post by Cameron Simpson
Post by Rob Cliffe
import os
def cls(): x=os.system("cls")
Now whenever you type
cls()
it will clear the screen and show the prompt at the top of the screen.
(The reason for the "x=" is: os.system returns a result, in this case
0.  When you evaluate an expression in the IDE, the IDE prints the
result.  So without the "x=" you get an extra line at the top of the
screen containing "0".)
Not if it's in a function, because the IDLE prints the result if it
        os.system("cls")
should be just fine.
Yes, you're right.
Rob Cliffe
Rob Cliffe
2024-06-08 19:18:16 UTC
Permalink
OK, here is the advanced version:
import os
class _cls(object):
    def __repr__(self):
        os.system('cls')
        return ''
cls = _cls()

Now when you type
cls
it clears the screen.  The only flaw is that there is a blank line at
the very top of the screen, and the ">>>" prompt appears on the SECOND
line.
(This blank line is because the IDLE prints the blank value returned by
"return ''" and adds a newline to it, as it does when printing the value
of any expression.)

Best wishes
Rob Cliffe
Michael F. Stemper
2024-06-10 13:55:20 UTC
Permalink
Post by Rob Cliffe
import os
        os.system('cls')
        return ''
cls = _cls()
Now when you type
cls
it clears the screen.  The only flaw is that there is a blank line at the very top of the screen, and the ">>>" prompt appears on the SECOND line.
(This blank line is because the IDLE prints the blank value returned by "return ''" and adds a newline to it, as it does when printing the value of any expression.)
Why have it return anything at all?
--
Michael F. Stemper
Indians scattered on dawn's highway bleeding;
Ghosts crowd the young child's fragile eggshell mind.
Stefan Ram
2024-06-10 14:32:41 UTC
Permalink
Post by Michael F. Stemper
Post by Rob Cliffe
import os
        os.system('cls')
        return ''
cls = _cls()
...
Post by Michael F. Stemper
Why have it return anything at all?
Because __repr__ needs to return a str.
Michael F. Stemper
2024-06-10 15:49:42 UTC
Permalink
Post by Stefan Ram
Post by Michael F. Stemper
Post by Rob Cliffe
import os
        os.system('cls')
        return ''
cls = _cls()
...
Post by Michael F. Stemper
Why have it return anything at all?
Because __repr__ needs to return a str.
Got it. Thanks for clarifying.
--
Michael F. Stemper
87.3% of all statistics are made up by the person giving them.
Alan Gauld
2024-06-09 18:06:07 UTC
Permalink
Post by Rob Cliffe
import os
        os.system('cls')
        return ''
cls = _cls()
Now when you type
cls
it clears the screen. 
For me on a Mac it clears the terminal screen that I used
to launch IDLE and prints a single blank line on the IDLE
shell. (And I have to use "clear" instead of "cls" of course.

A quick Google suggests that printing Ctrl-L (formfeed?) might
be a platform agnostic solution. But that didn't work for me
in IDLE either. I think this is one where the best bet is to go
into the IDLE code and add a Shell submenu to clear screen!
Apparently it's been on the workstack at idle-dev for a long
time but is considered low priority...
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
Loading...