Programing

파이썬의 유휴 창을 지우는 방법은 무엇입니까?

lottogame 2020. 6. 19. 19:25
반응형

파이썬의 유휴 창을 지우는 방법은 무엇입니까?


파이썬 콘솔에 대해 비슷한 주제가 있다는 것을 알고 있지만 그것들이 같은지 모르겠습니다. system ( "clear")을 시도했지만 여기서 작동하지 않았습니다.

파이썬의 유휴 창을 지우려면 어떻게합니까?


"cls"및 "clear"는 터미널 (예 : DOS 프롬프트 또는 터미널 창)을 지우는 명령입니다. 스크린 샷에서 IDLE 내의 셸을 사용하고 있으며 이러한 영향을받지 않습니다. 유감스럽게도 유휴 상태에서 화면을 지우는 방법이 없다고 생각합니다. 가장 좋은 방법은 다음과 같이 화면을 여러 줄 아래로 스크롤하는 것입니다.

print "\n" * 100

이것을 함수에 넣을 수는 있지만 :

def cls(): print "\n" * 100

그런 다음 필요할 때 호출하십시오. cls()


os.system('clear')리눅스에서 작동합니다. Windows를 실행중인 경우 os.system('CLS')대신 시도하십시오 .

먼저 다음과 같이 os를 가져와야합니다.

import os

ctrl+ L는 Ubuntu Linux에서 화면을 지 웁니다.


대부분의 대답은 여기서 명령이 아닌 DOS 프롬프트 화면을 지우는 것입니다. 다른 대답은 화면의 명확한 효과를 보여주기 위해 빈 줄을 인쇄하는 것이 었습니다.

이 질문에 대한 가장 간단한 대답은

외부 모듈을 통합하지 않으면 Python IDLE 셸을 지울 수 없습니다. 빈 순수한 신선한 껍질을 얻으려면 이전 껍질을 닫고 다시 실행하십시오.


셸을 지우는 확장은 Issue6143 에서 "기능 요청"으로 찾을 수 있습니다 . 이 확장은 IdleX에 포함되어 있습니다 .


>>> import os

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

그것은 완벽합니다. '0'도 인쇄되지 않습니다.


IDLE 'shell'버퍼를 지우는 방법이 없습니다.


Python 2.4 이상에서 명령을 실행하는 방법은 하위 프로세스 모듈 을 사용하는 것 입니다. 사용하는 것과 같은 방법으로 사용할 수 있습니다 os.system.

import subprocess
subprocess.call("clear") # linux/mac
subprocess.call("cls", shell=True) # windows

파이썬 콘솔에서 이를 실행하는 경우 변수에 할당하는 것과 같이 반환 값을 숨기려면 ( os.system또는 중 하나 subprocess.call) 무언가를 수행해야 합니다.

cls = subprocess.call("cls", shell=True)

나는 사용하고 싶다 :

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

clear()

파일-> 새 창

새 창에서 **

실행-> Python Shell

이 방법의 문제점은 변수와 같이 정의한 모든 내용을 지우는 것입니다.

또는 명령 프롬프트 만 사용해야합니다.

명령 프롬프트를여십시오

"cd c : \ python27"유형

"python example.py"를 입력하면 대화식 모드가 아닌 경우 IDLE을 사용하여이를 편집해야합니다. 파이썬 쉘에 있다면 파일-> 새 창.

example.py는 C : \ python27과 같은 디렉토리 또는 python이 설치되어있는 디렉토리에 있어야합니다.

그런 다음 여기에서 키보드의 위쪽 화살표 키를 누르면됩니다. example.py를 편집하고 CTRL + S를 사용한 다음 명령 프롬프트로 돌아가서 위쪽 화살표 키를 누르고 Enter 키를 누릅니다.

명령 프롬프트가 너무 복잡하면 "clr"을 입력하십시오.

"clr"명령은 명령 프롬프트에서만 작동하며 IDLE에서는 작동하지 않습니다.


MAC OS X의 경우 " command + L "

우분투의 경우 " control + L "

대화식 세션에서 마지막 줄을 지 웁니다.


Windows에서이를 수행하는 가장 좋은 방법은 명령 프롬프트 'cmd'를 사용하고 python 디렉토리에 액세스하는 것입니다. 시작 메뉴> 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

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.


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


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!


use this

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

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.


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.


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.


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?


This works for me in Windows:

print chr(12)


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.


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


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

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

참고URL : https://stackoverflow.com/questions/1432480/any-way-to-clear-pythons-idle-window

반응형