9

我正在尝试在 Python3 中编写控制台应用程序。

问题是我希望所有输出消息 EG: print("Status message") 都位于底部输入行的上方。

Status message 1
Status message 2
Status message 3
Console:> I want to type here while the output messages displayed

目前它看起来更像这样

Console:>  want to type here while the outStatus message 1
put messages displayed

反正有没有使用诅咒来做到这一点?

4

2 回答 2

3

尝试这个:

print chr(27)+'[2AOutput'

希望这是你所要求的。

抱歉,以上内容适用于 Python 2.7。我不确定 Python 3 版本是否

print(chr(27)+'[2AOutput')

将工作与否。

参考: http ://en.wikipedia.org/wiki/ANSI_escape_code

于 2013-01-13T03:16:27.807 回答
0

这可以使用提供控制台带内控制的 ANSI 转义序列来完成。这就是 curses 库在后端的工作方式。

这个序列应该适合你想要的,为了清楚起见分开:

print("\u001B[s", end="")     # Save current cursor position
print("\u001B[A", end="")     # Move cursor up one line
print("\u001B[999D", end="")  # Move cursor to beginning of line
print("\u001B[S", end="")     # Scroll up/pan window down 1 line
print("\u001B[L", end="")     # Insert new line
print(status_msg, end="")     # Print output status msg
print("\u001B[u", end="")     # Jump back to saved cursor position

请注意,这并非特定于 python 并且适用于大多数控制台。

参考:

http://xn--rpa.cc/irl/term.html - 非常适合没有诅咒的 TUI 编程,包括有关为 vim 使用单独缓冲区的信息,例如屏幕切换

https://invisible-island.net/xterm/ctlseqs/ctlseqs.html - 完整的 ANSI 转义标准,维基百科不包括关键序列

于 2022-02-11T22:53:14.547 回答