0

i have recently come across the print(str, end="\r") in python 3.8, and find it pairs well for a terminal style GUI if you don't want to learn/use a GUI import. One problem i have found is that you cant use this to overprint multiple lines in order in the way that I am using it.

asdf = 1
spin = ""
spin1 = ""
spin2 = ""
spin3 = ""
spin4 = ""

import time

while 1 != 2:
    if asdf > 4:
        asdf = 1

    if asdf == 1:
        spin = ("............/.........")
        spin1 =(".........../..........")
        spin2 =("..........X...........")
        spin3 =("........./............")
        spin4 =("......../.............")                       
    if asdf == 2:
        spin = ("......................")
        spin1 =("......................")
        spin2 =("....------+------.....")
        spin3 =("......................")
        spin4 =("......................")       
    if asdf == 3:
        spin = ("........\.............")
        spin1 =(".........\............")
        spin2 =("..........X...........")
        spin3 =("...........\..........")
        spin4 =("............\.........")
        
    if asdf == 4:
        spin = ("..........|...........")
        spin1 =("..........|...........")
        spin2 =("..........+...........")
        spin3 =("..........|...........")
        spin4 =("..........|...........")

    print(spin, end="\r")
    print("")
    print(spin1, end="\r")
    print("")
    print(spin2, end="\r")
    print("")
    print(spin3, end="\r")
    print("")
    print(spin4, end="\r")
    (asdf) = (asdf) + (1)
    time.sleep(1)

could someone help me

  • Does this answer your question https://stackoverflow.com/questions/6169217/replace-console-output-in-python? – bertdida Aug 10 '20 at 01:18
  • What exactly is your expected output? – 101 Aug 10 '20 at 01:19
  • bertdida, i am using that concept, but i want to replace multiple lines at once – Lachlan Earl Aug 10 '20 at 01:23
  • 101, my expected output is how the spin, spin1, spin2, spin3 & spin4 variables change as the asdf variable cycles through numbers 1-4. I know that with the current code that it will not work, I was wondering how I would be able to get it to work for future programs. – Lachlan Earl Aug 10 '20 at 01:24
  • Reconstruct the entire multiline string (a single string) for each condition instead of using multiple strings. [Pythonic way to create a long multi-line string](https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string) – wwii Aug 10 '20 at 01:57

2 Answers2

0

The last line is easy with '\r'. If you want to overwrite more than that, terminals have special escape codes to move the cursor anywhere, but it depends on the terminal type. To do it right you have to detect the terminal type and use the right escape codes for that type. Rather than implementing all that yourself, use curses. It's in the standard library.

A simple example would be with window.addstr()

import curses

screen = curses.initscr()  # Get the screen object.

# Write to buffer at these coordinates. (Line 5, column 10).
# It's probably easiest to do one of these per line.
screen.addstr(5, 10, "Hello, world!")


screen.refresh()  # Print the changes in the buffer to the screen.

The screen will remain under the control of curses until you give it back with curses.endwin().


If you happen to be using Windows, you'll need an extra binary for the standard library curses to work. The easiest way to add it is pip install windows-curses. (Other OSes usually just work.)

gilch
  • 10,813
  • 1
  • 23
  • 28
0

Most shells - if not everyone - provide a set of escape sequences for (among others) cursor movement. Since the most used shells are bash and zsh i will provide an example for both.

In bash the escape sequence for moving the cursor up is \033[<N>A where N is the number of lines:

print(spin)
print(spin1)
print(spin2)
print(spin3)
print(spin4)
(asdf) = (asdf) + (1)
time.sleep(1)
print("\033[6A") # moving the cursor up 6 lines

For more bash escape sequences look here

For zsh the sequence is \e[<N>A:

print(spin)
print(spin1)
print(spin2)
print(spin3)
print(spin4)
(asdf) = (asdf) + (1)
time.sleep(1)
print("\e[6A")

For more zsh escape sequences look here

Don Foumare
  • 444
  • 3
  • 13