Sujet : Re: IDLE: clearing the screen
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 04. Jun 2024, 16:19:01
Autres entêtes
Organisation : Stefan Ram
Message-ID : <console-20240604151710@ram.dialup.fu-berlin.de>
References : 1
Cave Man <
caveman@example.invalid> wrote or quoted:
I am new to Python, and I have been using IDLE (v3.10.11) to run small
Python code. However, I have seen that the output scrolls to the bottom
in the output window.
Is there a way to clear the output window (something like cls in command
prompt or clear in terminal), so that output stays at the top?
No, unless you patch IDLE to add such a feature!
(When I wrote [4] here recently, I had thought of just such cases!)
But you can implement a console yourself using tkinter.
As a motivating example see [1]. But as a beginner you may
not be able to modify [1] for your needs unless you learn
some tkinter first, for that I recommend [2] and [3].
[1]
import time
import tkinter as tk
class TextWrapper:
def __init__(self, master):
self.master = master
self.text = tk.Text(master)
self.text.pack()
def print( self, line: str )-> None:
"""Append a line to the Text component."""
self.text.insert( tk.END, line + "\n" )
self.text.see( tk.END ) # Scroll to the end
def reset( self )->None:
"""Clear the Text component."""
self.text.delete( 1.0, tk.END )
def put( line: str )-> None:
text_wrapper.print( line ); root.update(); time.sleep( 1 )
def cls()-> None:
text_wrapper.reset(); root.update(); time.sleep( 1 )
def execute_after_one_second():
put( "Hello, World!" )
put( "This is a test." )
put( "Another line." )
cls()
put( "Text has been reset." )
root = tk.Tk()
text_wrapper = TextWrapper(root)
root.after( 1000, execute_after_one_second )
root.mainloop()
[2]
the GUI part of "Programming Python" by Mark Lutz
[3]
"Python Tkinter" or "Modern Tkinter for Busy Python
Developers" by Mark Roseman
(He has a good tkinter tutorial on his web site)
[4]
We need somethin like a portable curses module (plus colorama)
and it has got to work on both Windoze and Linux straight out
of the box in standard Python. And it would be even sicker if it
could run in that IDLE console too! That way, we could code up
some legit terminal-based software (like slrn or Nethack) using
just plain ol' Python, no extra stuff required. It's a real
shame it ain't already part of the standard library. Someone's
got to step up and make this happen. Python needs a portable
console TUI!