Any way to clear python's IDLE window?

匿名 (未验证) 提交于 2019-12-03 07:50: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?

回答1:

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



回答2:

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:

ctrl + L clears the screen on Ubuntu Linux.



回答4:

An extension for clearing the shell can be found in Issue6143 as a "feature request". This extension is included with IdleX.



回答5:

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



回答6:

There does not appear to be a way to clear the IDLE 'shell' buffer.



回答7:

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) 


回答8:

>>> import os  >>>def cls(): ...    os.system("clear") ... >>>cls() 

That does is perfectly. No '0' printed either.



回答9:

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.



回答10:

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.



回答11:

If you want to clear the shell buffer go to shell > restart shell in IDLE. Your question is a little vague. Do you want to clear the shell buffer or are you trying to call the OS command?



回答12:

The best way to do it on windows is using the command prompt 'cmd' and access python directory the command prompt could be found on the start menu >run>cmd

C:\>cd Python27 C:\Python27>python.exe 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. >>>import os >>>os.system('cls')  #This will clear the screen and return the value 0 


回答13:

use this

for num in range(1,100):     print("\n") 


回答14:

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



回答15:

Your screenshot shows you're using IDLE. cls/clear won't work for you.



回答16:

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.



回答17:

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.



回答18:

This works for me in Windows:

print chr(12)



回答19:

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.



回答20:

I like to use:

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


回答21:

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


回答22:

If you are using the terminal ( i am using ubuntu ) , then just use the combination of CTRL+l from keyboard to clear the python script, even it clears the terminal script...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!