python - 如何在python 3中更改光标的位置

标签 python python-3.x

我在 Windows 10 上,我不想安装新模块(接受标准库解决方案)。我希望用户输入的文本从第三行的末尾开始。

我的代码:

print(" Enter password to unlock the Safe .\n\n password : \n\n\t2 attempts remaining .")
# code to move the cursor to the end of " password : " goes here
x = input()

输出:

enter image description here

想要的输出:

enter image description here

如果没有 colorama(不幸的是它是一个外部模块),ANSI 转义序列似乎也无法工作。

最佳答案

在 Windows 10 上,您可以使用 Console Virtual Terminal Sequences 中的 ANSI 转义序列。 .

在使用它们之前,您需要 subprocess.run('', shell=True)(在 Python 3.5 之前,使用 subprocess.call)。这些序列使您正在寻找的东西成为可能。

警告:同样是 Microsoft 原创,Erase in DisplayErase in Line 的文档有部分错误。参数12被颠倒了。

这应该可行(尽管实际上输入密码似乎破坏了布局):

import subprocess
subprocess.run('', shell=True)
print(' enter password : \0337', end='')
print('\n\n\t2 attempts remaining .\0338', end='')
x = input()

关于python - 如何在python 3中更改光标的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50667636/

相关文章:

python - 如何在 cython 中实现更好的循环速度性能?

python-3.x - 无法使用python在cdk中为dynamodb添加二级索引

python - 获取 Flask 应用程序中定义的所有路由列表

python - 如何在 wxpython html 窗口中插入动态字符串? (wx.html.html 窗口)

python - 将文本图像分离成组成字符图像

python - 使用 time.sleep 函数打印速度慢

python-3.x - 可选类型注释。检查是否为“无”后使用值?

python - Eclipse 控制台和 Python 中的编码问题

python - 尝试模拟 Counter,收到 TypeError : 'str' object is not callable

python - Python 3 中排序函数与比较函数的等效项