Clear PyCharm Run Window

前端 未结 8 1691
长发绾君心
长发绾君心 2020-11-29 07:10

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

相关标签:
8条回答
  • 2020-11-29 07:36

    This question is a bit old but I thought I would leave this here for other people who are wondering how to do this

    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:

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

      1. CMD + , (or Pycharm preferences);

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

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

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

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

    EDIT: 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')
    
    0 讨论(0)
  • 2020-11-29 07:36

    Pycharm Community Edition 2020.1.3

    You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console

    0 讨论(0)
提交回复
热议问题