Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change on-screen text
#1
Is there a way to make already-printed text change without using \n a million times?
Self-taught HTML, CSS, Python, and Java programmer
Reply
#2
Are you talking about the text in the shell or command prompt?
Reply
#3
Have you looked into the curses library?
Reply
#4
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.
Reply
#5
(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?
Self-taught HTML, CSS, Python, and Java programmer
Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 1,599 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  ANSI not working for change of text colors BliepMonster 10 3,350 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  find some word in text list file and a bit change to them RolanRoll 3 1,522 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Change Text Size in Console? ShakeyPakey 9 11,171 Dec-09-2021, 02:51 AM
Last Post: drvlwho
  [split] Change Text Size in Console? CodingKid 1 1,871 Jul-20-2021, 02:47 PM
Last Post: deanhystad
  change a text file Blue Dog 5 2,736 Aug-04-2020, 05:51 AM
Last Post: DeaD_EyE
  Use of input function to change screen background color in Turtles Oldman45 3 4,869 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  Change Text Color Output in Python bluethundr 2 8,722 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,171 Feb-06-2019, 01:25 AM
Last Post: woooee
  Full Screen Text I/O DavidAlanGay 4 3,800 Oct-24-2018, 06:26 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020