def send():
data = input()
print("Server:",data)
c.send(data.encode())
我正在做一个使者,想在他们的信息前加上名字。但是我找不到在输入之前清除输入内容的方法,这样消息才能开始重复。我尝试使用\ r,但是它似乎无法与输入配合使用,并且我找不到可以清除特定行的模块。
您可以给input()
函数一个参数,该参数将放置在实际输入之前。
def send():
data = input("Server:")
c.send(data.encode())
我认为您希望它看起来像这样:
bob: hey
msg> how are you?
当用户点击Enter时,应更改为:
bob: hey
bob: how are you?
您可以通过支持的端子上的ANSI escape sequences执行此操作。
data = input("msg> ")
# when the user hits enter, the cursor moves down a line and to the first column
print("\033[A", end="") # move the cursor up a line
print("\033[K", end="") # erase the line
print("bob:", data) # print something new in the place of the input