用Python在控制台中同时打印2行
我使用Python 3在控制台中输出2个进度条,如下所示:用Python在控制台中同时打印2行,python,multithreading,Python,Multithreading,我使用Python 3在控制台中输出2个进度条,如下所示: 100%|###############################################| 45%|###################### | 两条线在不同的线程中同时生长 线程操作很好,两个进度条都在执行它们的工作,但是当我想打印它们时,它们会在控制台中的一行上相互重叠打印。我只得到一行进度条,它在显示这两个进度条之间交替显示 有没有办法让这些进度
100%|###############################################|
45%|###################### |
两条线在不同的线程中同时生长
线程操作很好,两个进度条都在执行它们的工作,但是当我想打印它们时,它们会在控制台中的一行上相互重叠打印。我只得到一行进度条,它在显示这两个进度条之间交替显示
有没有办法让这些进度条在单独的行上同时增长?您需要一个CLI框架<代码>诅咒是在Unix上工作时的完美选择(这里有一个Windows端口:)
您是否使用
clint.progress.bar
或其他库来生成进度条?curses
。。。开始打印进度条时,记下光标位置,然后返回到相同的光标位置进行更新。@Paulo Scardine:我使用了进度条。进度条很好,我只需要我提到的那种特定的输出显示。谢谢,这就可以了。
import curses
import time
import threading
def show_progress(win,X_line,sleeping_time):
# This is to move the progress bar per iteration.
pos = 10
# Random number I chose for demonstration.
for i in range(15):
# Add '.' for each iteration.
win.addstr(X_line,pos,".")
# Refresh or we'll never see it.
win.refresh()
# Here is where you can customize for data/percentage.
time.sleep(sleeping_time)
# Need to move up or we'll just redraw the same cell!
pos += 1
# Current text: Progress ............... Done!
win.addstr(X_line,26,"Done!")
# Gotta show our changes.
win.refresh()
# Without this the bar fades too quickly for this example.
time.sleep(0.5)
def show_progress_A(win):
show_progress( win, 1, 0.1)
def show_progress_B(win):
show_progress( win, 4 , 0.5)
if __name__ == '__main__':
curses.initscr()
win = curses.newwin(6,32,14,10)
win.border(0)
win.addstr(1,1,"Progress ")
win.addstr(4,1,"Progress ")
win.refresh()
threading.Thread( target = show_progress_B, args = (win,) ).start()
time.sleep(2.0)
threading.Thread( target = show_progress_A, args = (win,)).start()