简体   繁体   English

Python如何在不清除用户输入的情况下清除屏幕?

[英]Python How to clear screen without clearing user input?

Currently I am trying to make a chat system, but to update the screen, I need to clear it, then print out the messages.目前我正在尝试制作一个聊天系统,但是要更新屏幕,我需要清除它,然后打印出消息。 Here is the code I am using, but it also clears the input that is already typed in by the user when the method clear() is ran:这是我正在使用的代码,但它也会在运行clear()方法时清除用户已经输入的输入:

def receive():
    with connection.cursor() as cursor:
        while True:
        sql = "SELECT * FROM chat"
        cursor.execute(sql)
        rows = cursor.fetchall()
        for row in rows:
            print(row[0]+": "+row[1])
        time.sleep(1)
        clear()

I am using multiprocessing, and here is where I am trying to take input:我正在使用多处理,这是我尝试输入的地方:

p = multiprocessing.Process(target=receive)

p.start()

with connection.cursor() as cursor:
    while True:
        ask = input()
        if ask == "cancel()":
            p.terminate()
            clear()
            main()
        with connection.cursor() as cursor:
            sql = "insert into chat (name, message) values (%s, %s)"
            cursor.execute(sql, (name, ask))
            connection.commit()

clear is defined as clear = lambda: os.system('clear') clear定义为clear = lambda: os.system('clear')

Any help as to keep the input from being erased would be appreciated!任何有关防止输入被删除的帮助将不胜感激!

使用“clear -x”而不是“clear”语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM