2009-11-13 15 views

risposta

4

Il modo migliore è quello di utilizzare un po 'di libreria esistente come ncurses. Tuttavia, è possibile provare una soluzione temporanea cancellando la console con la chiamata di sistema: system("cls");.

+0

è ("CLS") solo per Windows? – yodie

+0

Su Linux c'è "clear" – doc

+1

E OS X? – yodie

2

È possibile utilizzare VT100 codes per riposizionare il cursore su una linea più alta, quindi sovrascriverlo con lo stato aggiornato.

1

La libreria Curses offre un controllo potente per le interfacce utente della console.

3

Se si sta utilizzando Python, provare a utilizzare blessings. È un involucro davvero intuitivo attorno alle maledizioni.

semplice esempio:

from blessings import Terminal 

term = Terminal() 

with term.location(0, 10): 
    print("Text on line 10") 
with term.location(0, 11): 
    print("Text on line 11") 

Se si sta effettivamente cercando di attuare una barra di avanzamento, è consigliabile utilizzare progressbar. Ti farà risparmiare un sacco di \r cruft.

In realtà è possibile connettere le benedizioni e la barra di avanzamento insieme. Provare a eseguire questo:

sistema
import time 

from blessings import Terminal 
from progressbar import ProgressBar 

term = Terminal() 

class Writer(object): 
    """Create an object with a write method that writes to a 
    specific place on the screen, defined at instantiation. 

    This is the glue between blessings and progressbar. 
    """ 
    def __init__(self, location): 
     """ 
     Input: location - tuple of ints (x, y), the position 
         of the bar in the terminal 
     """ 
     self.location = location 

    def write(self, string): 
     with term.location(*self.location): 
      print(string) 


writer1 = Writer((0, 10)) 
writer2 = Writer((0, 20)) 

pbar1 = ProgressBar(fd=writer1) 
pbar2 = ProgressBar(fd=writer2) 

pbar1.start() 
pbar2.start() 

for i in range(100): 
    pbar1.update(i) 
    pbar2.update(i) 
    time.sleep(0.02) 

pbar1.finish() 
pbar2.finish() 

multiline-progress

Problemi correlati