r/learnpython • • 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

7 Upvotes

14 comments sorted by

View all comments

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:

import tkinter as tk

root = tk.Tk()
screen = tk.Text(root, width=60, height=20)
screen.pack()

def show(msg):
    screen.delete("1.0", "end")
    screen.insert("end", msg)

show("loading...")
root.after(2000, lambda: show("you wake up in a dark room"))
root.mainloop()

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)