Python Forum

Full Version: Change on-screen text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a way to make already-printed text change without using \n a million times?
Are you talking about the text in the shell or command prompt?
Have you looked into the curses library?
There are many ways... each one with it's own problems.
To create something that looks like a dialog the best option is to use curses although it is a full library that can be hard to learn.
If you want something really easy (just move the cursor and rewrite a char, maybe some colours), you might go with the ANSI escape codes but take into account that not all of them work in all the terminals (i.e. windows) and that is really easy to finish with a terminal in funny colours and writing garbage (typing "reset" and hitting enter usually fix it)
Here I have done a small example that writes an spinning character until you press Control-C:
#!/usr/bin/env python3
import time

spin = "\-//|"

print("Working...[ ]", end='')

n = 0
while True:
    try:
        c = spin[n]
        n = (n + 1) % len(spin)
        
        # Move the cursor 2 position back and print the c
        # \033 is the Escape sequence
        print(f"\033[2D{c}]", end='', flush=True)
        time.sleep(0.2)
    except KeyboardInterrupt:
        break
print("\nDone!")
The main tricks are:
  • Use the end='' to avoid print from adding a new line each time
  • Use '\033' to write the ESC char. You can use also '\u001b'
  • Force print to flush the stdout buffer or otherwise you might not see anything as it is waiting for a newline or to fill the buffer
In this case I am using the ESC[nD sequence with n=2 to move 2 characters backward the cursor.
(Jun-07-2018, 09:36 PM)Grok_It Wrote: [ -> ]Are you talking about the text in the shell or command prompt?
Yes
(Jun-07-2018, 10:07 PM)micseydel Wrote: [ -> ]Have you looked into the curses library?
Not yet.
(Jun-07-2018, 11:05 PM)killerrex Wrote: [ -> ]There are many ways... each one with it's own problems. To create something that looks like a dialog the best option is to use curses although it is a full library that can be hard to learn. If you want something really easy (just move the cursor and rewrite a char, maybe some colours), you might go with the ANSI escape codes but take into account that not all of them work in all the terminals (i.e. windows) and that is really easy to finish with a terminal in funny colours and writing garbage (typing "reset" and hitting enter usually fix it) Here I have done a small example that writes an spinning character until you press Control-C:
 #!/usr/bin/env python3 import time spin = "\-//|" print("Working...[ ]", end='') n = 0 while True: try: c = spin[n] n = (n + 1) % len(spin) # Move the cursor 2 position back and print the c # \033 is the Escape sequence print(f"\033[2D{c}]", end='', flush=True) time.sleep(0.2) except KeyboardInterrupt: break print("\nDone!") 
The main tricks are:
  • Use the end='' to avoid print from adding a new line each time
  • Use '\033' to write the ESC char. You can use also '\u001b'
  • Force print to flush the stdout buffer or otherwise you might not see anything as it is waiting for a newline or to fill the buffer
In this case I am using the ESC[nD sequence with n=2 to move 2 characters backward the cursor.
I guess this could help?
(Jun-08-2018, 12:21 PM)Panda Wrote: [ -> ]
(Jun-07-2018, 09:36 PM)Grok_It Wrote: [ -> ]Are you talking about the text in the shell or command prompt?
Yes

I ask because perhaps your application has exceeded the capabilities of the console. I'd consider using tkinter to make a GUI application. Otherwise, curses is the way to go.