Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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 - Fatal编程技术网

在Python中追加文本和继续进程栏

在Python中追加文本和继续进程栏,python,Python,我正在编写一个代码来打印进度条,如:=============> 代码段: def run(self): event = self.event bar = '>' sys.stdout.write(bar) while not event.is_set(): bar = "=".join(["", bar]) sys.stdout.write(bar) sys.stdout.flush() ev

我正在编写一个代码来打印进度条,如:
=============>

代码段:

def run(self):
    event = self.event
    bar = '>'
    sys.stdout.write(bar)
    while not event.is_set():
        bar = "=".join(["", bar])
        sys.stdout.write(bar)
        sys.stdout.flush()
        event.wait(1)
这总是返回
=>=>==>===>===>
所以基本上它是打印>然后=>,=>


如何获得进度条输出,如
==========>

,这是因为您以“>”开始进度条,而不是在最后添加进度条,它应该按照您希望的方式工作,而且您实际上不需要进度条:

def run(self):
event = self.event
while not event.is_set():
    sys.stdout.write("=")
    sys.stdout.flush()
    event.wait(1)
sys.stdout.write(">")

您可以使用
\r
返回行首并覆盖它,例如:

import time
import sys

bar = '>'
for i in range(100):
    bar = '='.join(['', bar])
    sys.stdout.write('\r' + bar)
    sys.stdout.flush()
    time.sleep(0.5)

为什么要使用stdout.write而不是print?但是在打印完所有“=”之后,这将在最后打印“>”。我们不能把>和=?