Blessings: how to move cursor to bottom of screen on exit? - python

I'm trying out some cursor-graphical output of a Python script, to give my app a nice progress bar.
I use the following to ensure my screen is blank before displaying output.
print term.clear
But when my Python script finishes, my shell prompt is output at the top of the screen, overwriting part of the progress bar.
How can I move the cursor to below my graphical output as the Python script exits? I have tried this right before exiting the script:
print term.move(0, term.height-1)
But this seems to be ignored. My shell begins output at the top of the screen.
I don't want to enter fullscreen mode, I'd like to leave the final output of the progress bar on the screen after the script finishes.
My environment is:
OS X 10.10 Terminal in xterm-256 mode.
Python 2.7.10
Blessings 1.6

I think you have the arguments to term.move() backwards (based on the "Moving Permanently" section here).
move() Parameters are y coordinate, then x coordinate.
Try this:
print term.move(term.height - 1, 0)

Related

Python print not skipping to the end line in command prompt

I am writing a console program using python.
As you must be knowing, when a program is running and then a user scrolls up the command window, when the program prints something the window itself scrolls down to the end.
But when I do this print("\033[A\033[A") to remove the last printed line in the command prompt, it does not scroll down to the bottom and remove the last printed line but instead clears up the last line on the part of the screen that's visible to me.
What should I do to solve this?
Please help. Thank you!
I am on windows 10

How to clear pycharm output terminal with code? [duplicate]

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!

python auto clears everything on screen after 298 lines

my problem is that python.exe auto clears everything ive printed to it, after a certain amount of lines has been reached, for example i created the following program
a = 0
for a in range(0, 10000):
print(str(a) + ':> cola')
delay = input('BLARG :>')
now when i ran this in python .exe, i got the following result.
IMAGE1, and it carried on like this till it reached the end. IMAGE2 now the problem is python erased everything in the first image that came before the 9701st print which is a tad troublesome, does anyone have any advice on how to prevent python auto clearing everything.
This is not a python problem. change settings of your console ~v1k45
Windows' CMD you're using have a limit of old output it remembers.
I don't have Windows in English, so I do translation of names of the options myself, and they can differ from reality.
To change the limit, right-click on it's title bar and select Properties (for current window, or Defaults to change it for all future cmd windows), go to the tab Layout and increase heigth of Buffer size:

graphics.py GraphWin automatically closing

I'm new to Python and trying to learn graphics with John Zelle's graphics.py. I'm writing the Python scripts in MacVim and executing from the terminal (Mac OS 10.9.2) on Python 3. If I try to open a new window with GraphWin() the window opens briefly but then immediately closes.
Ex:
from graphics import *
win = GraphWin("circle",500,500)
c = Circle(point(100,100),30)
c.draw(win)
GUI elements with TkInter work just fine.
Any ideas why this might be happening?
Thanks!
If the statements you've shown in your question were entered as a script, and you just ran the script, then the problem is that the window is closed implicitly when the script ends. You will see in the very first example from the documentation (which also appears in the source code for graphics.py itself):
from graphics import *
def main():
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse() # Pause to view result
win.close()
main()
Notice that the author has specifically included a statement to pause before closing the window.
If, instead of a script, you just type the statements in one by one at an interactive Python prompt, then as long as that Python prompt is open, the graphics window stays open (until you explicitly close it, or close the Python session).

Ncurses, python, and OSX Lion

I'm new to nurses, and trying it out on my OSX Lion with some python code. I've ran across a weird bug, and I don't know what I'm doing wrong. I've Googled extensively, and can't find a similar issue, even in linux. I've selectively removed lines to see if one of them is an issue, also. When I run the code below, I get nothing. No menu, and my terminal is messed up, if I hit enter, you see what I get in the picture below. I have to type a reset to make it work well again. Can anyone give me suggestions, or point me in the direction where to look? I would really appreciate it. Thanks.
Script:
import curses
screen = curses.initscr() # Init curses
curses.noecho() # Suppress key output to screen
curses.curs_set(0) # remove cursor from screen
screen.keypad(1) # set mode when capturing keypresses
top_pos = 12
left_pos = 12
screen.addstr(top_pos, left_pos, "This is a String")
Result:
BTW, I'm using the default python and libs in Lion, no macports. I'd like to use the native libraries, if possible.
You have 2 problems.
After adding the string to the screen with addstr you don't tell it to refresh the screen. Add this after the call to addstr:
screen.refresh()
You need to call endwin() at the end of you program to reset the terminal. Add this to the end of your program:
curses.endwin()
That said, after making those 2 changes when you run your program it will appear to do nothing because after displaying the string on the screen curses exits and returns the screen to the state before you ran the program.
Add this before the call to endwin():
screen.getch()
Then it will wait for you press a key before exiting.

Categories

Resources