• IDLE: clearing the screen

    From Cave Man@21:1/5 to All on Tue Jun 4 19:04:17 2024
    Hello everyone,

    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?


    Thanks in anticipation!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Cave Man on Tue Jun 4 14:19:01 2024
    Cave Man <[email protected]d> 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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rob Cliffe@21:1/5 to All on Sat Jun 8 20:18:16 2024
    OK, here is the advanced version:
    import os
    class _cls(object):
        def __repr__(self):
            os.system('cls')
            return ''
    cls = _cls()

    Now when you type
    cls
    it clears the screen.  The only flaw is that there is a blank line at
    the very top of the screen, and the ">>>" prompt appears on the SECOND
    line.
    (This blank line is because the IDLE prints the blank value returned by
    "return ''" and adds a newline to it, as it does when printing the value
    of any expression.)

    Best wishes
    Rob Cliffe

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Michael F. Stemper on Mon Jun 10 14:32:41 2024
    "Michael F. Stemper" <[email protected]> wrote or quoted:
    On 08/06/2024 14.18, Rob Cliffe wrote:
    OK, here is the advanced version:
    import os
    class _cls(object):
        def __repr__(self):
            os.system('cls')
            return ''
    cls = _cls()
    ...
    Why have it return anything at all?

    Because __repr__ needs to return a str.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)