For some style in my console applications, I'll often have a string print out one character at at time using code like this:
import time
def run():
the_string = "Hello world!"
for char in the_string:
print(char, end='', flush=True)
time.sleep(0.1)
input()
run()
I am looking to do the same with Python Curses, so I can have some more flexibility in other aspects of the application. This is what I have so far:
import curses
import time
def run(stdscr):
stdscr.clear()
the_string = "Hello world!"
for char in the_string:
stdscr.addstr(char)
time.sleep(0.1)
stdscr.getch()
curses.wrapper(run)
problem is this just waits for the duration of the for loop before putting the text on the console. The difference is flush=True
so I've tried to include curses.flushinp()
at a few different places in the function, but it makes no difference.
CodePudding user response:
You need to call the stdscr.refresh()
after writing the string to the screen to refresh the screen.
import curses
import time
def run(stdscr):
stdscr.clear()
the_string = "Hello world!"
for char in the_string:
stdscr.addstr(char)
stdscr.refresh()
time.sleep(0.1)
stdscr.getch()
curses.wrapper(run)
works fine as you intended.