简体   繁体   English

如何在Python中清除屏幕?

[英]How to clear the screen in Python?

I know there are many other questions asking the same thing, but none of them work for me, and I can't comment asking for help. 我知道还有很多其他问题要问同样的事情,但是它们对我都不起作用,因此我无法评论寻求帮助。 They just don't work for me. 他们只是不为我工作。 I have tried all sorts of things! 我尝试了各种事情! I'm on a Mac, and using Pycharm with what I think is the most recent Python (v. 3), but I'm not sure. 我在Mac上,并且将Pycharm与我认为是最新的Python(第3版)一起使用,但我不确定。

1) Cmd + L does not work. 1)Cmd + L不起作用。

2) 2)

import os
clear = lambda: os.system('clear')
clear()
well = input("The screen is not clear.")

says "TERM environment variable not set." 说“未设置TERM环境变量”。

3) 3)

absolutely_unused_variable = os.system("clear")

says the same TERM thing. 说同样的术语。

4)This 4)该

    import subprocess, platform

    if platform.system()=="Windows":
        subprocess.Popen("cls", shell=True).communicate()
    else: #Linux and Mac
        print("\033c", end="")

prints ac for me in the final line. 在最后一行为我打印ac。

What am I doing wrong? 我究竟做错了什么? I tried all of the above, using all possible combinations of clear and cls. 我使用clear和cls的所有可能组合尝试了上述所有方法。 To be sure it wasn't my Pycharm I tried all on IDLE Python 3.7.2. 为了确保不是我的Pycharm,我在IDLE Python 3.7.2上尝试了全部。 I outright refuse to \\n 100 times. 我完全拒绝\\ n 100次。 Any suggestions? 有什么建议么? :) :)

In Unix-like systems a console window is a character device that can be anything from a GUI window an actual terminal. 在类Unix系统中,控制台窗口是字符设备,可以是GUI窗口或实际终端中的任何内容。 Different types of "devices" require different methods to clear the screen. 不同类型的“设备”需要不同的方法来清除屏幕。 Normally when a shell is started in a terminal the TERM environment variable is set to a value that indicates what type of terminal it is, and routines that clear the screen consult this in order to determine what characters to write in order to clear it. 通常,当在终端中启动外壳程序时,TERM环境变量将被设置为一个值,该值指示终端是什么类型的终端,并且清除屏幕的例程会参考此命令,以确定要写入的字符以清除它。

If you try your first solution in a Mac terminal window, it will clear the screen. 如果您在Mac终端窗口中尝试第一个解决方案,它将清除屏幕。 If you try it without even using Python, but unset the TERMINAL environment variable, then not even the system 'clear' command will know how to clear the screen: 如果您甚至没有使用Python都尝试过,但是未设置TERMINAL环境变量,那么即使系统的“ clear”命令也不会知道如何清除屏幕:

$ echo $TERM
xterm-256color
$ unset TERM
$ clear
TERM environment variable not set.

The problem you have is not specific to Python, but rather to the PyCharm terminal window, which does not appear to set this environment variable. 您遇到的问题并不特定于Python,而是特定于PyCharm终端窗口,该窗口似乎没有设置此环境变量。

It may be that the PyCharm terminal window does not support ANSI or other sequences for clearing the screen. PyCharm终端窗口可能不支持ANSI或其他清除屏幕的顺序。 It does appear to support some escape sequences for colored text, at least. 似乎至少支持彩色文本的某些转义序列。 It may be that it just doesn't set the environment variable correctly. 可能是因为它没有正确设置环境变量。 You could try export TERM=ansi in the terminal, and then try to run your clear command to see if that works. 您可以尝试在终端中export TERM=ansi然后尝试运行clear命令以查看是否有效。

Another thing you might try is using Click to clear the screen in a device- and platform-independent way. 您可能要尝试的另一件事是使用Click以与设备和平台无关的方式清除屏幕。 Click is pretty great in general. 一般来说,点击效果非常好。 The following, for example, works for me even after I unset TERM in a terminal: 例如,即使在终端中取消设置TERM之后,以下内容仍对我有用:

import click
click.clear()

I believe this topic is more related to PyCharm, you can create your own keyboard shortcuts in PyCharm for clearing the screen as if it were on console or cmd: 我相信这个主题与PyCharm更相关,您可以在PyCharm中创建自己的键盘快捷键,以清除屏幕,就像在控制台或cmd上一样:

  1. Open Pycharm preferences 打开Pycharm首选项
  2. Search: "clear all" 搜索:“全部清除”
  3. Double click "clear all" -> add keyboard shortcut (set it to CMD + L or whatever you'd like) 双击“全部清除”->添加键盘快捷键(将其设置为CMD + L或您想要的任何键)
  4. Make sure to click in the console (or Run window) before using the shortcut or it doesn't do anything. 使用快捷方式之前,请确保在控制台(或“运行”窗口)中单击,否则快捷方式将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM