向上移动控制台光标

2024-05-13 22:54:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在控制台左上角创建一个简单的时钟,每秒更新一次:

def clock():
    threading.Timer(1.0, clock).start()
    print('\033[0;0H' + time.asctime(time.localtime()))

我已经使用colorama包在Windows中启用了ANSI转义序列,但是转义代码似乎只是将光标移动到指定的像素数,而不是移动到指定的位置。


如何将光标移到位置(0, 0)


Tags: timewindowsdef时钟startcoloramaansiprint
1条回答
网友
1楼 · 发布于 2024-05-13 22:54:53

行和列从1开始,而不是从0开始。

print('\033[1;1H' + time.asctime(time.localtime()))

或更短

print('\033[H' + time.asctime(time.localtime()))

您可能还需要使用ESC-7和ESC-8保存和恢复位置。

有关代码列表,请参见http://ascii-table.com/ansi-escape-sequences-vt-100.php

巴里

相关问题 更多 >