Python 2.7 - Completely clear Console, including the input line? - python

I've tried the solution here - How to clear the interpreter console? - to clear the Console, and it works, but it still leaves the C:/Python27> line. Is there any way of clearing everything, including this? I'm new to Python, I'm trying to make a simple Noughts and Crosses game, for which I want a clear Console. I usually use VB.NET, where there are no input lines and this isn't a problem.

If this was me, I would see if I could perhaps control the size of the window being used, such that I know the exact number of lines on screen, and then print an exact number of blank lines such that the command prompt is not longer shown.
After this point, your noughts and crosses game I assume will print to screen multiple times, where each 'time' is going to be a new 'frame' of your game.

Related

How to delete the last OUTPUT in python console, not the last LINE?

I'm making a simplistic text adventure game for practice. I want players to be able to backtrack once they go farther into a scenario, for example after they go down a staircase I want them to be able to have "back" as an option and be able to retrace their steps.
I've already figured out every aspect of that except: How to clear the message that popped up telling them about the scenario they just entered.
I have code making their response a variable, and then determining if that variable is one of a set list of viable options, and if not looping them back to the start and deleting their previous entry with the line of code print("\033[A \033[A")
However, this code only deletes the last line of code, which varies wildly depending on screen ratio and different devices, and not the last output, which is the entire message that described to them the scenario.
Does anyone know a piece of code that can either
delete the entire last output of the console, in this case a printed line of code which exceeds one line and may extend for as many as 8-10 lines, detailing their scenario, or
clear the console and then warp them back to my predefined starting point (Example: def start0():), because after I clear the console the code start0() doesn't work.

Why are tkinter windows/boxes closing erratically and stopping the program?

To cut a long story short, I've been doing an interactive GUI (tkinter) word-game program for school. At first, everything went smoothly, but having finished the code, it has started to behave in unexpected ways when I run it. Some dialog boxes (particularly the
if tkinter.messagebox.askyesno():
thingy) just rapidly answer themselves with the 'no' option, rather than waiting for user input. Sometimes, the windows close off completely and cause the whole program to quit. However, although these errors are all the same (i.e. tkinter windows closing/answering themselves/stopping the program before they should), they usually happen in different places every time. I'm not sure if that's to do with the fact that tkinter is nested, opened, re-opened and closed numerous times within other code, which is making it run messily, but I have only destroyed tkinter windows in the right places, as far as I know.
Part of my code involves a while loop - I'm not sure if that could be interfering with the mainloop()s, but I couldn't find another way to allow the user to repeat the game as many times as they want.
I know this question is vague, but I'm mainly looking for tips - if it would be easier to diagnose if I split it up into different sections and tidy it up a bit, found an alternative for the while loop, etc.
Thanks!
TKinter dialogs should be fully completed and the results stored before moving onto the next section of code.
Make sure you provide all the arguments to the dialog (your example doesn't include the parameters).
result = tkinter.messagebox.askyesno('Confirm', 'Do you want to do this')
if result == true:

Spyder - hints disappear too fast

So I'm using Spyder as my Python IDE. It has a great feature which are hints, f.e when I type numpy.arange( it shows me, that I need to insert stop, start, step etc. But it appears on screen, and disappears after like 2-3s, and most of the times I don't manage to read the whole thing, but anyways I would still like to see it, just to think about what should I type. So is there a way to extend the timeout of those hints, or make them stay there until f.e I close the parentheses?
P.S Am I having delusions, or is IPython interpreter much faster than simple Python command line interpreter?
P.S2 Is there a way, to make Spyder do auto-indentation (f.e after going to a new line inside of a function?)
FryninoS,
If you put your mouse over the information box it will stay open until you move the mouse off the box.
Austin.

Python hide already printed text

I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:
opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.
clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)
I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!
EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.
EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to #zondo.
Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively printing strings.
curses is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses tutorial in the official Python documentation.
I'd personally just use this.
import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"
I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.
If you are on windows you must first import colorama a module that makes windows prompt be able to use the ANSI codes as such:
import colorama # OR: from colorama import init
colorama.init() # AND THEN: init()
So if your board has n rows, after the user input for their turn, you move the cursor UP n rows + however many were required for user input, so if you wrote Input row, col: ... then you would go UP n+1, etc...
A simple example:
numLines = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI.
I personally like tkinter myself.
You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.
Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.

Clear terminal up until point x - Python

A program I'm writing takes user commands from input(), executes corresponding functions, and displays relevant text.
After about 5 commands-worth of text, the terminal becomes cluttered even when the window is maximized. What I would like to do is clear the terminal after every five commands, but only clear the text that precedes (is above) the fifth command and its output text.
More specifically, after the user has typed in the fifth command, upon pressing Return (entering the command), I want commands 1-4 and their corresponding outputs to clear off the screen but have command 5 and its output remain at the top of the terminal.
For demonstration, here is what I want the screen would look like during this process:
The above becomes the below:
Using the os module and os.system('cls') or os.system('clear') functions will not exactly work in this situation. I don't want to clear all of the text on the screen, just the text before a certain point.
So, how can I do this on Windows with Python?
Note: If the solutions are simple, I would like both a method of obliterating the text so that it cannot be scrolled back up to as well as a method that would allow users to see previous commands and text.
Using simple terminal output, there isn't really a good way to do this. Even the operation of "clearing the screen" is outside what is normally considered simple terminal output, which is why you end up calling an external program to do it.
However, a different way of handling terminal output is to use the curses library. This library allows you extensive control over exactly how your output appears on the screen, and in fact includes functions like deleteln and insdelln to delete lines of text from the screen.

Categories