Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change on-screen text
#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


Messages In This Thread
Change on-screen text - by Panda - Jun-07-2018, 06:11 PM
RE: Change on-screen text - by Grok_It - Jun-07-2018, 09:36 PM
RE: Change on-screen text - by micseydel - Jun-07-2018, 10:07 PM
RE: Change on-screen text - by killerrex - Jun-07-2018, 11:05 PM
RE: Change on-screen text - by Panda - Jun-08-2018, 12:21 PM
RE: Change on-screen text - by Grok_It - Jun-08-2018, 03:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,115 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 3,125 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  ANSI not working for change of text colors BliepMonster 10 7,344 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  find some word in text list file and a bit change to them RolanRoll 3 2,387 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Change Text Size in Console? ShakeyPakey 9 15,244 Dec-09-2021, 02:51 AM
Last Post: drvlwho
  [split] Change Text Size in Console? CodingKid 1 2,512 Jul-20-2021, 02:47 PM
Last Post: deanhystad
  change a text file Blue Dog 5 3,704 Aug-04-2020, 05:51 AM
Last Post: DeaD_EyE
  Use of input function to change screen background color in Turtles Oldman45 3 8,273 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  Change Text Color Output in Python bluethundr 2 9,651 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 7,217 Feb-06-2019, 01:25 AM
Last Post: woooee

Forum Jump:

User Panel Messages

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