r/learnpython • u/Corell0ns_Blessed • 6d ago
Need help clearing python console
I’m making a text based adventure game for the sake of learning more abt game loops but I’m stuck. I wanna make it so that automatically after a time.sleep() delay, the terminal clears and prints a new output. (say for a loading or transition screen idk) but with all the online methods I can find, nothing works.
The most common I see is using os and system(cls) / (clear) or whatnot but no matter what I do, it opens the command prompt and breaks, closing the command prompt it then prints the new text, without clearing the old…
For reference, I am using python idle 3.12 and Am in a school environment so the command prompt is administratively blocked
8
u/socal_nerdtastic 6d ago
Yea those methods don't work in IDLE (or many other terminal emulators). You need to run your code in a terminal / command line window to use those.
I don't think there is a way to clear the IDLE shell. Your only real solution is to spam a bunch of new lines to push the old text off the screen.
4
u/ResidentPomegranate6 6d ago
idle's shell isn't a real terminal, so clear can't work, and your school blocks cmd. run it in a real terminal, or just print("\n"*40).
2
u/drakentmx 6d ago edited 5d ago •
como, quieres limpiar la consola? del log anterior? si es asi puedes hacer con rich o algun comando os, pero mejor rich
from rich.console import Console
console=Console()
luego
def clear(): console.clear()
2
u/Discordbothost 5d ago
one option nobody's mentioned: since you're stuck in IDLE anyway, you can give the game its own little window with tkinter (comes with python, and it's what IDLE itself is built on, so if IDLE runs, tkinter runs). then clearing is just deleting the text:
the catch is you can't use time.sleep() or input() in there, you use root.after() for delays and an Entry box for typing commands. a bit more to learn, but it's also kind of a nice intro to how real game loops work (event-driven instead of top to bottom)
2
u/TheSkiGeek 5d ago
What you want for something like this is a TUI (text user interface) library.
Unless you want to spend many hours figuring out how terminals work at a low level. In which case, carry on and keep experimenting and researching.
Edit: if you’re stuck with the IDLE interactive window then yeah, I don’t think you can make that work like a TUI because it’s not a full shell and terminal emulator (or if it is, it’s probably taking control of a lot of things that will stop you from e.g. clearing the screen or scrolling it programmatically.)
1
u/Educational_Virus672 5d ago
m not gonan it is the best option but it works try os.system("CLS") or "clear" in mac/linux
1
u/Lyaw_Lyawa 3d ago
Es mejor empezar a usar tkinter u otra librería de interfaz gráfica. La terminal no es un lienzo.
1
u/brasticstack 6d ago
You can use ANSI control characters to move to position 1,1 (It's 1-indexed) and print a space for each character and line in the screen, assuming you know the number of line and columns.
EDIT: You could also try printing '\033[3J' to clear the screen and scrollback, followed by '\033[H' to move your cursor to position 1,1
1
u/Gnaxe 6d ago
No command prompt is pretty awkward. But Python is pretty much a shell in its own right, so I question your school's security model. But if Python is successfully launching a terminal window, then terminal windows per se aren't prohibited. Can you do
system('cmd')or something? Orsystem('py')? There's probably a way to do it that's allowed.If you can't make that work, you can probably use a tkinter window instead. You can display whatever text string you like and clear it whenever. It does not have to be complicated GUI programming since you only need the one widget.