Sim*_*uin 12 pycharm python-3.x
有没有办法清除PyCharm中的"运行"控制台?我想要一个代码删除/隐藏之前制作的所有print().像"clear_all"按钮一样,但无需手动按下它.
我已经读过有一种方法可以在带有os.system("cls")的终端中进行,但在PyCharm中,它只添加一个小方块而不清除任何东西.
另外,我不想使用print("\n"*100),因为我不想回滚并查看以前的打印件.
You*_*wei 11
在Pycharm:
CMD + ,(或Pycharm偏好);CTRL + L或任何内容)如何
您可能需要先安装一些其他软件包
如果您使用 pip 从 PyPI 安装 PyAutoGUI:
Windows 没有依赖项。不需要安装 Win32 扩展。
OS X 需要安装 pyobjc-core 和 pyobjc 模块(按此顺序)。
Linux 需要安装 python3-xlib(或 Python 2 的 python-xlib)模块。Pillow 需要安装,在 Linux 上你可能需要安装额外的库以确保 Pillow 的 PNG/JPEG 正常工作。看:
- CMD + ,(或 Pycharm 首选项);
- 搜索:“全部清除”;双击->
- 添加键盘快捷键(将其设置为 CTRL + L 或任何东西)
- 在您的 Pycharm 控制台中享受这个新的热键吧!
然后,如果您将“全部清除”的键盘快捷键设置为 Command + L在您的 python 脚本中使用它
import pyautogui
pyautogui.hotkey('command', 'l')
Run Code Online (Sandbox Code Playgroud)
示例程序
这将在用户键入输入后清除屏幕。
如果您没有专注于工具窗口,那么您的清除热键将不起作用,如果您在专注于编辑器的同时尝试按下热键,您可以亲眼看到这一点,您将不会清除嵌入式终端内容。
PyAutoGUI 没有办法直接聚焦windows,解决这个问题可以尝试找到run终端所在的坐标,然后发送左键点击聚焦,如果你还不知道可以点击鼠标的坐标您可以使用以下代码找到它:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
Run Code Online (Sandbox Code Playgroud)
输出示例:
(2799, 575)
Run Code Online (Sandbox Code Playgroud)
现在是实际代码:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Run Code Online (Sandbox Code Playgroud)
小智 5
还有另一种方法可以使用 os.system 中的系统类来完成此操作。您需要做的就是拥有以下代码:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
Run Code Online (Sandbox Code Playgroud)
但是,为了使此功能在 pycharm 中工作,您需要启用“在输出控制台中模拟终端”。当您右键单击要添加此功能的文件并选择“修改运行配置”时,您可以找到它,然后它位于“执行”选项下。这是一个屏幕截图:pycharm screenho
| 归档时间: |
|
| 查看次数: |
20267 次 |
| 最近记录: |