Jun-07-2018, 06:11 PM
Is there a way to make already-printed text change without using \n a million times?
Self-taught HTML, CSS, Python, and Java programmer
Change on-screen text
|
Jun-07-2018, 06:11 PM
Is there a way to make already-printed text change without using \n a million times?
Self-taught HTML, CSS, Python, and Java programmer
Jun-07-2018, 09:36 PM
Are you talking about the text in the shell or command prompt?
Jun-07-2018, 10:07 PM
Have you looked into the curses library?
Jun-07-2018, 11:05 PM
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:
Jun-08-2018, 12:21 PM
(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:I guess this could help?#!/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:
Self-taught HTML, CSS, Python, and Java programmer
Jun-08-2018, 03:19 PM
(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. |
|