python - 如何在命令提示符下下推当前行中打印的文本?(Windows 上的 Python 3.8)
问题描述
考虑到以下程序:
from time import sleep
from threading import Thread
def speaker():
while True:
sleep(3)
print("[*]I'm speaking........")
if __name__ == "__main__":
my_speaker = Thread(target=speaker)
my_speaker.start()
while True:
msg = input("[Your message]>> ")
print("Your message: " + msg)
如果我没有在输入中输入任何内容('_' 是光标的位置),则输出:
[Your message]>> [*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
_
我想显示的输出:
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[*]I'm speaking........
[Your message]>> _
当 'speaker' 函数执行打印函数时,它应该执行以下步骤:
将当前行的文本移动到下一行
返回上一行的开头
执行打印
返回到下一行的末尾
我在这里看了一下:https ://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences 我发现了一些有用的东西,比如能够回到当前行的开头使用' \r ',但是我不想覆盖文本,而是将其移动到下一行。
有什么想法?
解决方案
尝试这个:
from time import sleep
from threading import Thread
def speaker():
while True:
sleep(3)
print("\r[*]I'm speaking........\n[Your message]>> ", end="")
if __name__ == "__main__":
my_speaker = Thread(target=speaker)
my_speaker.start()
while True:
msg = input("[Your message]>> ")
print("Your message: " + msg)
推荐阅读
- python - 来自 Python 的 PythonAnywhere 编辑(例如 VS Code)
- android - Android Volley 请求适用于模拟器,但在物理手机上出现客户端错误。伙计们请帮助我,我是新手
- javascript - 传单多边形问题
- laravel - 广播频道未在 Laravel 中注册
- next.js - Next js public/assets/css 不会加载到动态生成的页面上
- django - 用于可写嵌套 JSON 的 DRF 序列化器
- java - Apache HttpClient 4.5.13 java.net.SocketException:管道损坏(写入失败)
- swift - 反正有没有知道你在数组中的位置?迅速?
- node.js - 将我的 npm 从版本 6 升级到版本 7(节点版本为 10)后,我无法为某些项目安装依赖项
- java - 如何在JAVA中从谷歌存储云桶中获取所有pdf文件