使用 os.system("cls") 清除屏幕不适用于套接字

Clearing the screen with os.system("cls") dosn't work with socket

所以我正在制作一个 Python 多人 Ascii 游戏,我需要清除游戏的框架,但是当我想使用 os.system('cls') 它什么都不做。

import socket,time
import subprocess as sp
import keyboard,os

def Main(k):
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect(("127.0.0.1",8000))
    while True:
        for Key,Command in k.items():
            if keyboard.is_pressed(Key):
                exec(Command)

        data = s.recv(2048).decode()
        data = data.replace("PlaceHolder","")
        lastdata = data
        s.send("-".encode())

        if not data == "":
            print(data+"\n"*2)
        elif data != "" and lastdata == "":
            sp.call('cls')

if __name__ == '__main__':
    keydict = {"w":'s.send("w".encode())',
               "d":'s.send("d".encode())',
               "a":'s.send("a".encode())',
               "s":'s.send("s".encode())',
               "esc":'exit()'}
    Main(keydict)

我在我的代码中犯了错误并修正了它们。 旧:

if not data == "":
        print(data+"\n"*2)
    elif data != "" and lastdata == "":
        sp.call('cls')

新:

if not data == "":
        os.system("CLS")
        print(data+"\n"*2)
        time.sleep(0.05)

所以我删除了无用的 if 语句,因为它没有触发 os.system("CLS")