Reputation: 213
I'm connecting via Linux Mint's Xfce terminal ssh (also tried ssh -t) to a Raspberry Pi running Rasperian Stretch. On the Pi I have a Python-Curses example which I modified trying to get the current time to update every few seconds "in place" over writing itself. But it prints the time once initially and then never prints an updated time. But if I manually press "Enter" key on my laptop the time updates, if I press "Enter" fast multiple times the "enters" stack-up and the time shows updates for as many times as I pressed enter. There's probably something maybe basic I don't understand about Curses and terminals or Python in this case. Appreciate it. Here's code:
import curses
import traceback
import datetime
import time
try:
# -- Initialize --
stdscr = curses.initscr() # initialize curses screen
curses.noecho() # turn off auto echoing of keypress on to screen
curses.cbreak() # enter break mode where pressing Enter key
# after keystroke is not required for it to register
stdscr.keypad(1) # enable special Key values such as curses.KEY_LEFT etc
# -- Perform an action with Screen --
stdscr.border(0)
stdscr.addstr(5, 5, 'Hello from Curses!', curses.A_BOLD)
stdscr.addstr(6, 5, 'Press q to close this screen', curses.A_NORMAL)
while True:
# stay in this loop till the user presses 'q'
#stdscr.addstr(7, 5, 'zztop',curses.A_NORMAL)
ti = str((datetime.datetime.now().time()))
stdscr.addstr(8, 5, 'Time: '+ ti, curses.A_NORMAL)
time.sleep(3)
ch = stdscr.getch()
if ch == ord('q'):
break
# -- End of user code --
except:
traceback.print_exc() # print trace back log of the error
finally:
# --- Cleanup on exit ---
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
Upvotes: 3
Views: 3263
Reputation:
You need to call stdscr.refresh()
before sleeping to flush output to the screen.
You may also want to add stdscr.nodelay(1)
to the initialization sequence in your program (i.e, right below stdscr.keypad(1)
). This will make calls to stdscr.getch()
non-blocking. Alternatively, you can call stdscr.timeout(3000)
instead and remove the call to sleep
entirely.
Upvotes: 2