2014-01-20 31 views
10

我使用Python 3輸出2的進度條在這樣的控制檯:打印2號線在控制檯同時在Python

100%|###############################################|  
45%|######################       | 

這兩個酒吧在單獨的線程同時增長。

線程操作很好,兩個進度條都在做他們的工作,但是當我想打印出來時,他們會在控制檯的一行上打印彼此的頂部。我剛剛看到一行顯示這兩個進度條的進度條。

這些進度條可以同時在不同的線上生長嗎?

+0

您是否使用'clint.progress.bar'或其他一些庫生成進度條? –

+3

'curses' ...開始打印條時記下光標位置,然後返回到相同的光標位置以更新它。 – tripleee

+0

@Paulo Scardine:我用[this](https://github.com/coagulant/progressbar-python3)進度條。進度條沒問題,我只是想要我提到的那種特定的輸出顯示。 – Haiku

回答

3

您需要一個CLI框架。 Curses是完美的,如果你是在Unix工作(並有針對Windows的端口,可以在這裏找到:https://stackoverflow.com/a/19851287/1741450

enter image description here

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() 
+0

謝謝,這是工作。 – Haiku