Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在接受未被删除和回显的输入时打印到屏幕上_Python_Multithreading_Input_Output_Simultaneous - Fatal编程技术网

Python 如何在接受未被删除和回显的输入时打印到屏幕上

Python 如何在接受未被删除和回显的输入时打印到屏幕上,python,multithreading,input,output,simultaneous,Python,Multithreading,Input,Output,Simultaneous,我发现了以下代码: import threading from time import sleep import os class test: def __init__(self): self.running = True def foo(self): while(self.running): os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] ) sle

我发现了以下代码:

import threading
from time import sleep
import os
class test:
    def __init__(self):
        self.running = True
    def foo(self):
        while(self.running):
            os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
            sleep(2)
    def getUserInput(self):
        x = ''
        while(x != 'e'):
            x = raw_input('Enter value: ')
        self.running = False
    def go(self):
        th1 = threading.Thread(target=self.foo)
        th2 = threading.Thread(target=self.getUserInput)
        th1.start()
        th2.start()
t = test()
t.go()

但每次它清除屏幕时,它都会清除所有内容,包括我键入的内容。我需要一些可以清除屏幕的东西,但可以让我键入,而当屏幕清除时,我键入的内容无法清除。我需要屏幕每两秒钟清除一次,并在任何时候接受输入。

您所采用的方法无法工作,因为像cls这样的工具不知道在其他地方发生的原始输入()之类的事情,因此它们无法保留一些屏幕内容。你需要的是协调这一点。我认为有两种可能的办法:

  • 等待输入时不清除屏幕,即在请求用户输入时只向清理线程发出等待信号
  • 使用低级库访问screenbuffer,从而保留提示。我认为Python有它的“诅咒”实现,应该允许您这样做

但你应该做的是退一步,在更抽象的层面上描述你想要实现的目标。你在问如何实现一个从一开始就有缺陷的解决方案。如果您描述了最初的目标,则有人会想出一种效果更好的方法。

键入后清除屏幕上的内容。。。我不认为你想有一个清晰的屏幕在自己的线程。。。