1

I know there are already many questions on this, so my question will be considered as duplicate but I am asking because I tried all most all of them and none of them worked.

I have a Hangman code, written in R which I am trying to convert into python. In R whenever a user enters a letter guess, my code outputs a Hangman's ASCI art and it does after clearing the console every time with this code cat('\f'). Which I am trying to replicate in Python but I don't found any working code.

I searched for this on google and tried many things,

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

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

Answer from Clear/overwrite standard output in Python

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

Source lost!

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)

This code snippet is from this question Clear PyCharm Run Window

All of them worked on the command prompt but none of them in pycharm. They show a weird box with a question mark

you can see how it looks like when I perform it

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

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

output

enter image description here

However, I tried this code snippet, which worked but the problem here is that my program takes input and while I am giving input, it detects my mouse position( which will be obviously on the terminal, not the trach icon, while typing) and clicks on the terminal. so overall it doesn't worked as well

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
Darkstar Dream
  • 1,649
  • 1
  • 12
  • 23
  • 1
    Have you tried os.system('clear')? Also is it really important for it to work in pycharm specifically? – pudup May 07 '21 at 13:08
  • Yeah, as I have mentioned above, I have also tried that code. And about the pycharm thing, I want to do that there because I want to share my code with others and I won't be able to convenience everyone to use it on the terminal. Also, I know that this code works fine in the terminal so I am curious about the pycharm because, you won't be using cmd everytime right! – Darkstar Dream May 07 '21 at 16:06
  • If it doesn't work even manually, you have to investigate why that is. Just trying to send the keystrokes for CTRL+L from Python won't achieve anything otherwise. It would also be helpful to provide a list of links of things you've tried and explain exactly what you did and how they didn't work. Otherwise your question risks getting closed as a duplicate for something that hasn't worked for you. – Reti43 May 07 '21 at 21:03
  • @bad_coder as I mentioned in my question, the third code snippet is from the link you provided and it didn't work. That's why I am asking separately – Darkstar Dream May 07 '21 at 23:08
  • @Reti43 you are right, that I need to first solve the problem of CNTRL+L( that why it does not work even manually), but seriously I don't know much about pycharm technical. Can you suggest to me some sources or steps to find the issue – Darkstar Dream May 07 '21 at 23:10
  • In one of the links you referenced, from where you got the pyautogui code snippet, the answer covers how to set the hotkey for clear all. Have you tried that? – Reti43 May 08 '21 at 03:08
  • @Reti43, yeah I have tried, but it didn't work probably because it doesn't work manually. should I contact with pycharm community – Darkstar Dream May 08 '21 at 04:09
  • If the directions in that question, or even [this page](https://www.jetbrains.com/help/pycharm/configuring-keyboard-and-mouse-shortcuts.html) don't help, it might be time to debug it with the help of the community. – Reti43 May 08 '21 at 04:17
  • @Reti43 how can I do that? – Darkstar Dream May 08 '21 at 04:21

0 Answers0