Python 使用 curses lib getstr() 函数的交互式控制台不会等待用户输入
Python interactive console using curses lib getstr() function dosen't wait for user input
我正在尝试使用 curses 库创建交互式控制台。
类似于此:
import curses
if __name__ == '__main__':
stdscr = curses.initscr()
stdscr.nodelay(False)
curses.echo()
curses.nonl()
while True:
stdscr.addstr('#')
stdscr.refresh()
command = stdscr.getstr()
if not command:
continue
if command == b'xys':
stdscr.addstr('Execute xys block and print result')
elif command == b'qwe':
stdscr.addstr('Execute qwe block and print result')
# ...
但是 return 这个 :
#xys
Execute xys block and print result#
#qwe
Execute qwe block and print result#
#
#
#
(在 Execute qwe block and print result#
行之后我按下了 enter
键)
stdscr.getstr()
函数只有一半时间等待输入。另一半command = ''
没有任何输入。这就是为什么您会在每个输出的末尾看到双 #
符号或 #
符号。
预期的结果应该是:
#xys
Execute xys block and print result
#qwe
Execute qwe block and print result
#
有什么推荐吗?
我正在使用 pyCharm 终端(选中 run
> edit configurations...
> emulate terminal in output console
)但显然 curses lib 和 pyCharm 不兼容.
当运行普通终端中的脚本时,它运行完美。
希望这对某人有所帮助!
我正在尝试使用 curses 库创建交互式控制台。
类似于此:
import curses
if __name__ == '__main__':
stdscr = curses.initscr()
stdscr.nodelay(False)
curses.echo()
curses.nonl()
while True:
stdscr.addstr('#')
stdscr.refresh()
command = stdscr.getstr()
if not command:
continue
if command == b'xys':
stdscr.addstr('Execute xys block and print result')
elif command == b'qwe':
stdscr.addstr('Execute qwe block and print result')
# ...
但是 return 这个 :
#xys
Execute xys block and print result#
#qwe
Execute qwe block and print result#
#
#
#
(在 Execute qwe block and print result#
行之后我按下了 enter
键)
stdscr.getstr()
函数只有一半时间等待输入。另一半command = ''
没有任何输入。这就是为什么您会在每个输出的末尾看到双 #
符号或 #
符号。
预期的结果应该是:
#xys
Execute xys block and print result
#qwe
Execute qwe block and print result
#
有什么推荐吗?
我正在使用 pyCharm 终端(选中 run
> edit configurations...
> emulate terminal in output console
)但显然 curses lib 和 pyCharm 不兼容.
当运行普通终端中的脚本时,它运行完美。
希望这对某人有所帮助!