【问题标题】:How to push down the text printed in the current row in command prompt? (Python 3.8 on Windows)如何在命令提示符下下推当前行中打印的文本? (Windows 上的 Python 3.8)
【发布时间】:2020-04-07 22:28:29
【问题描述】:

考虑以下程序:

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' 返回到当前行的开头,但是我不想覆盖文本而是将其移到下一行。

有什么想法吗?

【问题讨论】:

    标签: python multithreading ansi-escape


    【解决方案1】:

    试试这个:

    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)
    

    【讨论】:

    • 我也试过这个解决方案,如果你没有在输入中写任何东西,它就可以工作,问题就是:[*]I'm speaking........\n [Your message]>> Hello wo如果'speaker'函数现在执行打印函数,它将覆盖“你好哇”。
    • 不确定您的意思。一切似乎都在我的身上正常工作。我确实在显示的第一个“[您的消息]”中输入了“Hello wo”,它在下一行打印了“Your message: Hello wo”。而且扬声器()也一直在运行......当我不插入任何东西作为输入时也能完美运行。
    • 所以我猜这是否是 Windows 命令提示符所拥有的限制。抱歉,无法将其复制为我的操作系统仅 Linux。希望此链接对您有所帮助:superuser.com/questions/1325654/…
    • 感谢您的回答和链接。抱歉,如果我没有清楚地解释问题,我的意思是:如果您尝试键入“Hello wo”并等待扬声器()函数运行而不按 Enter,打印函数将覆盖“Hello wo”即使该字符串尚未保存在 msg 中。所以,如果你尝试写“rld”然后按回车,即使你显然只输入了“rld”,msg 也会包含字符串“Hello world”。
    【解决方案2】:

    我发现的问题是 sleep(3) 的位置让你的输入出现在你想要的之前,因为你想要等待的是输入本身。

    下一个问题是,如果你这样做了,你的输入最终会出现,但它也会显示类似“[Your message]>> [*]I'm speak...... ."。我认为这是因为您几乎同时执行输入和循环,这有点“变得疯狂”,所以我添加了一个简单的 opt 触发器以防止打印工作并最终将输入输入到下部没有任何奇怪的结果。

    这是对您的问题的简单粗略的解决方案。

    我不认为它是完美的,我很确定有更好的选择来解决这个问题。我自己也是编码新手,但我希望我的解决方案能帮到你!

    from time import sleep
    from threading import Thread
    
    
    
    def speaker():
        while True:
            if opt is not None:
                print ("[*]I'm speaking........")
    global opt                   
    opt = 1
    
    if __name__ == "__main__":
        my_speaker = Thread(target=speaker)
        my_speaker.start()
        while True:
            sleep(3)
            opt = None
            msg = input("[Your message]>> ")
            print("Your message: " + msg)
    

    【讨论】:

    • 感谢您的回复,但问题不是何时打印[*]I'm speaking........,而是当我在输入中写入内容并执行“扬声器”功能时如何在终端中正确格式化文本打印。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    相关资源
    最近更新 更多