【问题标题】:Python terminal chat : how to avoid having messages overwrite my current unsent message in terminal?Python终端聊天:如何避免消息覆盖终端中当前未发送的消息?
【发布时间】:2019-09-03 02:04:47
【问题描述】:

我正在我的终端上使用 Python 在多个客户端之间创建一个简单的聊天。 问题是,每当我输入消息时,它都会被终端中的传入消息覆盖。然而,这只是视觉上的,当完成我的消息并按下回车键时,仍然会发送我的整个消息。 如何避免我的消息被覆盖?

我尝试使用以下输出调整代码行,目的是在某些创建的新行上打印任何传入消息:

sys.stdout.write("\033[F") #back to previous line
print("\n") #create a new line between second last and last line
print(msg) #print on new line any incoming messages

或者这样:

print("\r") #go back to line start
print("\n") #create a new line after last line (this also pushes any unfinished message to this new last line)
sys.stdout.write("\033[F") #back to previous line (empty now)
print(msg) #print on new line any incoming messages

但这实际上并没有按预期工作。 我还尝试仅在输入缓冲区(=最后一行?)为空时写入接收到的消息。我没有这个代码了,因为它不起作用,但它类似于:

while sys.stdin.buffer.read():
    #do nothing
print(msg)

【问题讨论】:

  • 您需要为您的聊天入口位置设置一个固定位置。使用curses 是构建此类输入应用程序的最简单方法。这是一个类似的问题,只有 python stackoverflow.com/questions/14300245/…
  • @Abhishet Dujari 如果你愿意,我可以接受你对这篇文章的回答,但我不能接受你的评论。

标签: python input terminal output chat


【解决方案1】:

您需要为您的聊天入口位置设置一个固定位置。使用 curses 是构建此类输入应用程序的最简单方法。这是一个类似的问题,只有 python Python console application - output above input line

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2015-03-28
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多