Any way to clear python's IDLE window?

2019-01-04 17:05发布

I know there's a similar topic about 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?

24条回答
一夜七次
2楼-- · 2019-01-04 17:47

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
查看更多
不美不萌又怎样
3楼-- · 2019-01-04 17:48
from subprocess import call as c

def cls():

    c("cls",shell=True)

print([x for x in range(10000)])

cls()

Whenever you wish to clear console call the function cls, but note that this function will clear the screen in Windows only if you are running Python Script using cmd prompt, it will not clear the buffer if you running by IDLE.

查看更多
疯言疯语
4楼-- · 2019-01-04 17:52

Most of the answers appear here do clearing the DOS prompt screen clearing commands. Which is not the Question asked here. An other answer here posted was the 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

查看更多
劫难
5楼-- · 2019-01-04 17:53

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/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)
查看更多
叛逆
6楼-- · 2019-01-04 17:53

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).

查看更多
叼着烟拽天下
7楼-- · 2019-01-04 17:54

I like to use:

import os
clear = lambda : os.system('cls') # or clear for Linux

clear()
查看更多
登录 后发表回答