I have a code, and I want to be able to clear the run window after certain actions are performed, however I haven't been able to find a way to clear it aside from clearing it yourself using the 'clear all' button(PyCharm btw). Is it possible to do that?
It seems that you can't clear the pycharm terminal without the clear button, however running it in cmd, the os.system('cls') or os.system('clear') works and will clear the screen.
You can't clear the pycharm window without the clear all button, but the os.system('clear') will clear the window if you run it in cmd instead so use that instead..
Related
I want to clear the screen time to time using function in Pycharm. When I tried to run the commands continuously, the screen gets full and seems clumsy.
I've tried the following commands used for CMD Mode and it is not working with Pycharm.
def clear():
os.system('cls')
As far as I am aware, Pycharm has no clear screen for it's console.
Whenever you restart the script that is running, the console will be cleared.
You can also do something like this,
print('\n'*80) # prints 80 line breaks, to emulate clearing the screen
I'm working on a script using the PyAutoGUI module. Sometimes the script gets stuck in a while loop because it's looking for pictures/images that are not shown due to connection problems etc. If this happens I want the program to start again from zero, so I want to simulate the play/run-button in Pycharm with a command line. Is this possible?
What it sounds like you want to do is restart your program if it doesnt respond. A similiar question appears to have been posted here: Python help - Need the ability to restart the script when it hangs or automatically set a timer so I would reccommend having a look at that. Simulating the run button in Pycharm might seem like a good idea at first but it is very specific and a bad practice to simulate user actions like that unless there is absolutely no viable alternative.
Thanks to Patel I managed to solve my issue. You can use ctrl+f5 to restart a script so now when I'm stuck in a while loop I'm using this code:
#Click toolbar Pycharm
pyautogui.moveTo(1672,15,1)
pyautogui.click()
#Rerun the script
pyautogui.hotkey("ctrl","f5")
I wrote this piece of code and tried to debug it:
from time import *
for i in range (100):
sleep(1)
print(i)
I first run this script in debug mode, and try to pause it by clicking the pause button, but the pause button doesn't work at all, it just keep printing new numbers.
Then I directly run this script (not in the debug mode), the pause did stop pycharm from printing new numbers, but the script is actually still running in the background, when I resume the script, it prints a lot of numbers all of a sudden.
So how can I correctly pause the script execution?
I installed pycharm and python in a whole new windows 7, it still behaves like this.
The stop and rerun button works perfectly, breakpoints too. But the pause button never works.
The pause ("Pause Output") button only temporarily suspends output to the terminal - it has no effect on the script execution. You may wish to use debug mode with breakpoints instead.
You can add breakpoints into your program by clicking in the space to the left of the text editor (the "Left Gutter", where line numbers appear, if you have them enabled).
See the Pycharm documentation for more information.
Update 2022
We now have a pause button in debug mode :
Hello I found a little issue with using the turtle library and python's IDLE. Observe the following code written in Python 3.5.2:
import turtle
turtle.Turtle()
input("Try moving/resizing the window in IDLE. Press enter than try again.")
Before pressing enter to give your input, the turtle window will be labeled as unresponsive and not allow the user to resize it. That is, if it's ran with IDLE. However, it works perfectly fine when ran through something such as the windows command prompt or PowerShell.
If ran through IDLE, after the user presses enter the window may then be freely moved and resized with no issues. This comes with the drawback that we're no longer inside the script but back to python's shell. Of course, we can still interact with the turtle via the shell but this isn't what I want in my actual application. My main program uses input() to ask the user if they wish to move/rotate and by how much. This remains in an endless loop until the user enters a phrase such as 'quit' to end the program. I will note, even with IDLE the turtle still moves and is drawn correctly. The problem is that the window itself becomes unresponsive. The contents is all correct.
I'm very curious to as of why only IDLE is giving me such behavior... Not only that, is there a way I can keep turtle window from being unresponsive and still make use of input() and the like? Or perhaps there is an alternative I did not think of? My research has lead me to believe this has something to do with TkInter. That would explain why it only works outside of IDLE.
Below is an image that shows my predicament.
Unresponsive turtle window
As you suspected, the primary issue is with tkinter, not specifically with the turtle app that uses tkinter. I first reproduced in IDLE with a bare tk window.
import tkinter as tk
root = tk.Tk()
input('prompt: ')
I then did the above a line at a time in 3.5.2 running interactively in Command Prompt on Win10. The second line displays a bare window, add a entry in TaskManager, and adds a Python icon to the task bar. Clicking back and forth between CP and Tk causes the the corresponding window and icon to get the 'active' appearance. The window border shifts from gray to black, whereas the icon background shifts from black to gray.
While typing the input statement, but before hitting return, the Tk window shows 'not responding' (after a couple of seconds) both on the title bar and in TaskManager. The mouse over the inside of the window becomes the blue busy circle. The window becomes 'active' as described above, and can be moved, but cannot be resized and cannot be closed normally (clicking on [X] brings up the "Not responding, close or wait?" box.
When the statement is Entered, the tk window becomes normally responsive again. As soon as the 'user' enters anything, the tk window becomes unresponsive in the manner described above, until entry is completed with Enter.
If you retry in CP and enter some characters without hitting Enter, do you see the same unresponsiveness?
Differences in IDLE's shell: creating root puts a new entry in TaskManager, but the new window is attached to the IDLE icon. While the input statement is being written, the Tk window remains normally responsive. So at this stage, IDLE is better. When the input() statement is Entered, the Tk window becomes, as you noted, completely unresponsive (cannot move) even before any entry. This part is worse.
I tried one further experiment: running IDLE with the -n ('no subprocess') option.
C:\Users\Terry>python -m idlelib -n
In this mode, there is no problem that I could detect (unlike CP). This was still true when I ran turtle. You can ignore the deprecation warning for now.
Comment 1: IDLE is designed for development and learning, not for production execution. But it can be used for the latter if there is an advantage to doing so. You would just have to check that using -n does not introduce any other problems, or rather, that turtle and your code do not interfere with IDLE when running in the same process.
Comment 2: GUI programs usually do not use input and print. They are usually run without a console to interact with. If input() is used, even in the Windows console, user should not touch the window before responding to a prompt.
Comment 3: GUI programs usually use GUI widgets instead to fetch and display info. Turtle makes this more difficult, but not impossible, as demonstrated by turtledemo. You might be simplify and adapt the code in turtledemo/main.py.
I've seen some apps allow you to show/hide the console when you need to read log messages. For example Blender3D allows that (blender.org).
I was wondering if this can be done in Python and how.
My main window is a Panda3D (panda3d.org) window.
I've read somewhere that one option is to hide the "real" console (pythonw) and create another console and just redirect everything from the "real" one to it, every time you want to "show" the "real" console. No idea how this can be done.
Or at least a way to choose whether to start the program with the console or without it by reading a configuration file or something.
I'm assuming you are talking about Windows because this console toggling in blender is Windows exclusive. I'm guessing Blender uses GetConsoleWindow and ShowWindow on Windows.
This is how you could do it in python with pywin32:
import win32gui, win32console, win32api, win32con
import time
console_window = win32console.GetConsoleWindow()
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_HIDE)
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_SHOW)
time.sleep(1)
If you run this program with python and not pythonw it will show the console, sleep for a second, hide the console, sleep for another second and then hide it again.
Mind that this code only works on Windows. On other platforms silly stuff like this is not necessary because if you want a program to show a console then you run it from the console.