我正在使用Linux Mint的Xfce终端ssh(也尝试过ssh -t)连接到运行Rasperian Stretch的树莓派。在树莓派上,我有一个Python-Curses示例,我修改了它以尝试每隔几秒钟“原地”更新当前时间而不是覆盖它。但它只打印了一次时间,然后就再也没有打印更新的时间了。但如果我手动按下笔记本电脑上的“Enter”键,时间就会更新;如果我快速按下多次“Enter”,则“Enter”将堆叠起来,时间将显示出我按下的那么多次数。也许关于Curses和终端或者Python方面,有些基础的东西我没有理解。感谢您的帮助。以下是代码:
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()