清除 PyCharm 运行窗口 [英] Clear PyCharm Run Window

查看:178
本文介绍了清除 PyCharm 运行窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法清除 PyCharm 中的运行"控制台?我想要一个删除/隐藏之前制作的所有 print() 的代码.类似于clear_all"按钮,但无需手动按下.

我已经读到有一种方法可以在终端中使用 os.system("cls") 来完成,但在 PyCharm 中,它只添加了一个小方块而没有清除任何内容.

另外,我不想使用 print("\n" *100) 因为我不想能够回滚并看到以前的打印.

解决方案

如何

  1. 下载这个包https://github.com/asweigart/pyautogui.它允许 python 发送击键.

您可能需要先安装一些其他软件包

<块引用>

如果您使用 pip 从 PyPI 安装 PyAutoGUI:

Windows 没有依赖项.Win32 扩展不需要已安装.

OS X 需要安装 pyobjc-core 和 pyobjc 模块(在那个订单).

Linux 需要 python3-xlib(或 Python 2 的 python-xlib)模块安装.Pillow 需要安装,在 Linux 上你可能需要安装额外的库以确保 Pillow 的 PNG/JPEG 正常工作.见:

  1. 如 Taylan Aydinli 所述,在 pycharm 中设置用于清除运行窗口的键盘快捷键

<块引用>

  1. CMD + ,(或 Pycharm 首选项);

<块引用>

  1. 搜索:全部清除";双击 ->

<块引用>

  1. 添加键盘快捷键(将其设置为 CTRL + L 或任何其他键)

<块引用>

  1. 在您的 Pycharm 控制台中享受这个新的热键吧!

  1. 然后,如果您将全部清除"的键盘快捷键设置为 Command + L 在您的 Python 脚本中使用它

     导入 pyautoguipyautogui.hotkey('command', 'l')

示例程序

这将在用户输入输入后清除屏幕.

如果您没有专注于工具窗口,那么您的清除热键将不起作用,如果您在专注于编辑器的同时尝试按下热键,您可以亲眼看到这一点,您不会t 清除嵌入的终端内容.

PyAutoGUI 没有办法直接聚焦windows,解决这个问题可以尝试找到run终端所在的坐标,然后左键点击聚焦,如果你还不知道坐标在哪里可以单击鼠标,您可以使用以下代码找到它:

导入pyautogui从时间导入睡眠睡觉(2)打印(pyautogui.position())

输出示例:

(2799, 575)

现在是实际代码:

导入pyautogui为真:input_1 = 输入(?")打印(输入_1)pyautogui.click(x=2799, y=575)pyautogui.hotkey('command', 'l')

Is there a way to clear the "Run" console in PyCharm? I want a code that delete/hide all the print() made previously. Like the "clear_all" button, but without having to press it manually.

I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.

Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.

解决方案

How to

  1. Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.

You may have to install some other packages first

If you are installing PyAutoGUI from PyPI using pip:

Windows has no dependencies. The Win32 extensions do not need to be installed.

OS X needs the pyobjc-core and pyobjc module installed (in that order).

Linux needs the python3-xlib (or python-xlib for Python 2) module installed. Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:

  1. Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli

  1. CMD + , (or Pycharm preferences);

  1. Search: "clear all"; Double click ->

  1. Add keyboard shortcut (set it to CTRL + L or anything)

  1. Enjoy this new hot key in your Pycharm console!

  1. Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script

     import pyautogui
     pyautogui.hotkey('command', 'l')
    

Example program

This will clear the screen after the user types an input.

If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.

PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:

import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())

An example of output:

(2799, 575)

and now the actual code:

import pyautogui

while True:
    input_1 = input("?")
    print(input_1)
    pyautogui.click(x=2799, y=575)
    pyautogui.hotkey('command', 'l')

这篇关于清除 PyCharm 运行窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆