如何用代码清除pycharm输出终端?

时间:2021-05-07 12:45:23

标签: python terminal pycharm operating-system

我知道这方面已经有很多问题,所以我的问题将被视为重复,但我问的是因为我尝试了所有这些问题,但没有一个奏效。

我有一个用 R 编写的 Hangman 代码,我试图将其转换为 Python。在 R 中,每当用户输入字母猜测时,我的代码都会输出 Hangman 的 ASCI 艺术,并且每次使用此代码 cat('\f') 清除控制台后都会输出。我试图在 Python 中复制它,但我没有找到任何工作代码。

我在谷歌上搜索了这个并尝试了很多东西,

import os
def clear():
    os.system('cls')
clear()
# or
import os

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

来自Clear/overwrite standard output in Python的回答

print('\033[H\033[J')

源丢失!

import pyautogui

while True:
    input_1 = input("?")
    print(input_1)
    pyautogui.hotkey('command', 'l')
# I changed the clear all hotkey to CNTRL+L (but i doesn't work even when I use it manually after 
# clicking on console)

此代码片段来自此问题 Clear PyCharm Run Window

所有这些都在命令提示符下工作,但在 pycharm 中没有一个。他们显示了一个带有问号的奇怪框

你可以看到我执行它时的样子

import os
def clear():
    os.system('cls')

print('\n Hey there'*2)
clear()
print('\n Bye'*2)

输出

enter image description here

但是,我尝试了这个代码片段,它有效,但这里的问题是我的程序接受输入,当我提供输入时,它会检测我的鼠标位置(显然在终端上,而不是在trach图标上,而打字)并点击终端。所以总的来说效果不太好

import pyautogui

# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # this line was not added in original source
print(mousepos) # prints current pos of mouse

# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it

0 个答案:

没有答案
相关问题