您需要一個CLI框架。 Curses
是完美的,如果你是在Unix工作(並有針對Windows的端口,可以在這裏找到:https://stackoverflow.com/a/19851287/1741450)
![enter image description here](https://i.stack.imgur.com/aKsXv.png)
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()
您是否使用'clint.progress.bar'或其他一些庫生成進度條? –
'curses' ...開始打印條時記下光標位置,然後返回到相同的光標位置以更新它。 – tripleee
@Paulo Scardine:我用[this](https://github.com/coagulant/progressbar-python3)進度條。進度條沒問題,我只是想要我提到的那種特定的輸出顯示。 – Haiku