Reputation: 105077
I know there's a similar topic about the Python console, but I do not know if they are the same. I tried system("clear")
and it didn't work here.
How do I clear Python's IDLE window?
Upvotes: 213
Views: 667693
Reputation: 117
Command + L for Mac OS X.
Ctrl + L for Ubuntu.
It clears the last line in the interactive session.
Upvotes: 7
Reputation: 988
You can make an AutoHotkey script.
To set Ctrl + R to a hotkey to clear the shell:
^r::SendInput print '\n' * 50 {Enter}
Just install AutoHotkey, put the above in a file called idle_clear.ahk, run the file, and your hotkey is active.
Upvotes: 2
Reputation: 57
The best way to do it on windows is using the command prompt 'cmd' and access the Python directory. The command prompt could be found on the start menu → Run → cmd.
C:
cd C:\Python27
python.exe
Output:
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
Session:
>>>import os
>>>os.system('cls') #This will clear the screen and return the value 0
Upvotes: 2
Reputation: 261
Use:
>>> import os
>>>def cls():
... os.system("clear")
...
>>>cls()
That does it perfectly. A '0' isn't printed either.
Upvotes: 22
Reputation: 95941
As mark.ribau said, it seems that there is no way to clear the Text widget in IDLE. One should edit the EditorWindow.py
module and add a method and a menu item in the EditorWindow class that does something like:
self.text.tag_remove("sel", "1.0", "end")
self.text.delete("1.0", "end")
and perhaps some more tag management of which I'm unaware of.
Upvotes: 3
Reputation: 27581
The way to execute commands in Python 2.4+ is to use the subprocess module. You can use it in the same way that you use os.system
.
import subprocess
subprocess.call("clear") # Linux and Mac
subprocess.call("cls", shell=True) # Windows
If you're executing this in the Python console, you'll need to do something to hide the return value (for either os.system
or subprocess.call
), like assigning it to a variable:
cls = subprocess.call("cls", shell=True)
Upvotes: 6
Reputation: 114943
os.system('clear')
works on Linux. If you are running Windows try os.system('CLS')
instead.
You need to import os first, like this:
import os
Upvotes: 113
Reputation: 271
This answer is for IDLE, not for the command prompt and was tested with Python 3.10.6.
If you press Ctrl+Z while the code is running (before it finishes), the previous output will be erased. If you wish to automate this, there's the pynput package.
pip install pynput
Here's a sample code (macros are unsafe, use it at your own risk):
# License: MIT-0
import time
import pynput
class _Eraser:
keyboard = pynput.keyboard.Controller()
ctrl = pynput.keyboard.Key.ctrl
is_initialized = False
@classmethod
def erase(cls, n):
if not cls.is_initialized:
cls.is_initialized = True
n += 1
for _ in range(n):
with cls.keyboard.pressed(cls.ctrl):
cls.keyboard.press('z')
cls.keyboard.release('z')
time.sleep(0.1)
def erase(n=1):
_Eraser.erase(n)
print('test1\n', end='')
print('test2\n', end='')
erase() # Erase 'test2\n'
print('test3')
print('test4')
erase() # Erase '\n'
print('test5')
print('test6')
erase(2) # Erase '\n' and then 'test6'
print('test7')
The output is:
test1
test3
test4test5
test7
Upvotes: 0
Reputation: 71
Turtle can clear the screen.
#=====================================
import turtle
wn = turtle.Screen()
wn.title("Clear the Screen")
t = turtle.Turtle()
t.color('red', 'yellow')
t.speed(0)
#=====================================
def star(x, y, length, angle):
t.penup()
t.goto(x, y)
t.pendown()
t.begin_fill()
while True:
t.forward(length)
t.left(angle)
if t.heading() == 0: #===============
break
t.end_fill()
#=====================================
# ( x, y, length, angle)
star(-360, 0, 150, 45)
t.clear()
#=====================================
Upvotes: 0
Reputation: 119231
The "cls" and "clear" are commands which will clear a terminal (ie a DOS prompt, or terminal window). From your screenshot, you are using the shell within IDLE, which won't be affected by such things. Unfortunately, I don't think there is a way to clear the screen in IDLE. The best you could do is to scroll the screen down lots of lines, eg:
print ("\n" * 100)
Though you could put this in a function:
def cls(): print ("\n" * 100)
And then call it when needed as cls()
Upvotes: 114
Reputation: 2334
Most of the answers, here do clearing the DOS prompt screen, with clearing commands, which is not the question. Other answers here, were printing blank lines to show a clearing effect of the screen.
The simplest answer of this question is
It is not possible to clear python IDLE shell without some external module integration. If you really want to get a blank pure fresh shell just close the previous shell and run it again
Upvotes: 68
Reputation: 377
It seems like there is no direct way for clearing the IDLE console.
One way I do it is use of exit() as the last command in my python script (.py). When I run the script, it always opens up a new console and prompt before exiting.
Upside : Console is launched fresh each time the script is executed. Downside : Console is launched fresh each time the script is executed.
Upvotes: 5
Reputation: 1
There is no need to write your own function to do this! Python has a built in clear function.
Type the following in the command prompt:
shell.clear()
If using IPython
for Windows, it's
cls()
Upvotes: -9
Reputation: 375
I would recommend you to use Thonny IDE for Python. It's shell has "Clear Shell" option and you can also track variables created in a separate list. It's debugging is very good even comparing with modern IDEs. You can also write code in python file along with access to shell at the same place.
And its lightweight!
Upvotes: 1
Reputation: 4969
It seems it is impossible to do it without any external library.
An alternative way if you are using windows and don't want to open and close the shell everytime you want to clear it is by using windows command prompt.
Type python
and hit enter to turn windows command prompt to python idle (make sure python is installed).
Type quit()
and hit enter to turn it back to windows command prompt.
Type cls
and hit enter to clear the command prompt/ windows shell.
Upvotes: 3
Reputation: 367
None of these solutions worked for me on Windows 7 and within IDLE. Wound up using PowerShell, running Python within it and exiting to call "cls" in PowerShell to clear the window.
CONS: Assumes Python is already in the PATH variable. Also, this does clear your Python variables (though so does restarting the shell).
PROS: Retains any window customization you've made (color, font-size).
Upvotes: 4
Reputation: 369
I like to use:
import os
clear = lambda : os.system('cls') # or clear for Linux
clear()
Upvotes: 6
Reputation: 961
An extension for clearing the shell can be found in Issue6143 as a "feature request". This extension is included with IdleX.
Upvotes: 26
Reputation: 51
File -> New Window
In the new window**
Run -> Python Shell
The problem with this method is that it will clear all the things you defined, such as variables.
Alternatively, you should just use command prompt.
open up command prompt
type "cd c:\python27"
type "python example.py" , you have to edit this using IDLE when it's not in interactive mode. If you're in python shell, file -> new window.
Note that the example.py needs to be in the same directory as C:\python27, or whatever directory you have python installed.
Then from here, you just press the UP arrow key on your keyboard. You just edit example.py, use CTRL + S, then go back to command prompt, press the UP arrow key, hit enter.
If the command prompt gets too crowded, just type "clr"
The "clr" command only works with command prompt, it will not work with IDLE.
Upvotes: 5