Clear screen with CMD disabled - python-3.x

I'm trying to clear the screen in my Idle text editor (the default one that comes with python (and is basically command prompt anyway).
I have had this problem for a while now and i've searched just about every corner of the web but nothing has truly answered my problem.
import subprocess
import subprocess as sp
import os
os.system('cls')
does not work
Ctrl + L does not work
clearing modules also do not work
Using this for loading screens, animations etc... would be a dream com true so if anyone can find a way to do this it would be much appreciated.

Related

subprocess works but unexpected black screen

I'm trying to open the DellCommandUpdate with python in full screen. I can get the application to open with the first code but couldn't figure out how to maximize the screen. Someone suggested trying sub process. It comes up as a black window similar to cmd.
import os
os.startfile("C:\Program Files (x86)\Dell\CommandUpdate\DellCommandUpdate.exe")
import subprocess
subprocess.call(["cmd", "/c", "start", "/max", "C:\Program Files (x86)\Dell\CommandUpdate\DellCommandUpdate.exe"])
The goal for this is to be able to open the program to be able to click update. I'm not sure if there is something like selenium but for desktop apps or how I would program to automate this

disable failSafeCheck Python PyAutoGui without editing the pyautogui file

So I'm using PyCharm as a python editor, and I don't know if it's because of my editor, but whenever I run a simple PyAutoGui program such as:
import time, pyautogui
time.sleep(3)
while True:
pyautogui.hotkey('ctrl', 'r')
time.sleep(15)
for too long, I keep getting an error (I don't have that error currently, because I usually have to wait a long time before getting the error from pyautogui. But it looks something like) :
PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen.
I'm not very experienced with Python PyAutoGui module, but is there a way to disable that safe-check without doing something in the pyautogui python file?
You're in luck! Just add pyautogui.FAILSAFE = True right after importing the library.
Check out their docs it has a lot of good info.

pyttsx3 is conflicting with tkinter

My tkinter app will run console-free (.pyw) until I import pyttsx3. As soon as pyttsx3 is imported, the app will only run from the editor (Idle).
This is a tkinter app that runs perfectly when run from idle. I import pyttsx3, initialize it, have it speak using Windows Sapi voices, all is well, all tkinter functions operate as intended from start to finish. But outside of Idle, the app won't run in .pyw mode. It shows a black console screen for a brief moment and closes. I have checked very carefully - removing all pyttsx3 code from the app - except the import statement and, quite literally, the import statement alone is enough to cause the app to no longer run in .pyw mode.
import tkinter as tk
(runs fine in .pyw mode)
import tkinter as tk
import pyttsx3 as speak
(will not run in .pyw mode)
The question: how could simply importing a library (not even initializing or using it...just importing it) cause the tkinter app to no longer run as .pyw? Could importing a library somehow be interfering with the tkinter main loop?
Good question. If I had to guess, something in the pyttsx3 library invokes a process that is unrelated to Python, for the purpose of text-to-speech conversion. Windows probably opens a Command Prompt window in such a case since that process runs independently.
Unless the pyttsx3 library has documentation for how to suppress this—after a cursory glance, I don't see such—then I would recommend opening a new issue with the package maintainer. I believe that it would need to set the CREATE_NO_WINDOW flag when being run on Windows.

tkinter set window on bottom of all other windows as desktop widget

I'm using Python3 on ubuntu gnome 16.04.
I wrote a whole UI project using tkinter and now I'm stuck because I cant find a way to solve the next issue:
My question:
is there any way to set a window on the bottom of all other windows using tkinter?
so that when I press 'win' + 'd' shortcut and go back to the desktop I will see my widget running?
edit:
If tkinter does not support that, I would be glad to hear other solutions
to make my widget run on the bottom of other windows, in a way that my main GUI library will still be tkinter (I don't want to refactor all my project because on that) maybe adding bash commands that pin my window to the desktop, maybe using some other python window attributes libraries.

Clear PyCharm Run Window

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.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
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
How to
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:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
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')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
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()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
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 # storing mouse position
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
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!

Resources