Paste the progress bar of the single-line refresh implementation first:
For a single-row refresh of the console, it is relatively straightforward to post the code directly:
1Strarrs = ['/','|','\\']2 forIinchRange (15):3Sys.stdout.write (strarrs[i% 3]+'{}/15:'. Format (i+1) +'#'* i+'\ r')4 Sys.stdout.flush ()5Time.sleep (1)
For single-row refreshes and pits
1.print is a friendly package for sys.stdout.write, using Sys.stdout.write to output data to standard output, seemingly also can be done with print, there is no in-depth study
2. Be sure to include ' \ R ' in write (), or an escape character such as ' \b ', where the Python escape characters are as follows:
Escape Character |
Description |
\ (at end of line) |
Line continuation character |
\\ |
Backslash symbol |
\‘ |
Single quotation marks |
\" |
Double quotes |
\a |
Bell |
\b |
BACKSPACE (Backspace) |
\e |
Escape |
\000 |
Empty |
\ n |
Line break |
\v |
Portrait tab |
\ t |
Horizontal tab |
\ r |
Enter |
\f |
Page change |
\oyy |
Octal number yy represents the character, for example: \o12 represents a newline |
\xyy |
The decimal number yy represents the character, for example: \x0a represents a newline |
\other |
Other characters are output in normal format |
This is equivalent to a carriage return to the beginning of the output, then, re-write, in the refresh!
In this way, a simple progress bar is achieved!
Next say, multi-line refresh problem:
Multi-line refresh, you can not simply use the input and output, should be used is a curses library, this library, is previously written in C library, if familiar with C, the use of curses library, with Python will get started quickly.
But I didn't touch Luo before. 1.1 points to see Luo-, and one point is this library, now seems to be only used under Linux, Windows can not use, Mac is not very clear, Windows seems to have another solution
On
Most of the implementation curses, the program pattern is based on the following code
stdscr=# Some common settings Curses.noecho () curses.cbreak () Stdscr.keypad(1# The following write the listening button code or loop code, etc. .. .... # to close previous actions
The main program logic is ... , paste the code directly below:
ImportCursesImport TimeImportOSImportRANDOMSTDSCR=CURSES.INITSCR () Curses.noecho ( )#no output--Curses.cbreak ()#Read now: not clear--Stdscr.keypad (1)#Open Keypadstdscr.box () Width=os.get_terminal_size (). Columnsheight=os.get_terminal_size (). Linesc_y= height//2-1c_x= width//2-10Stdscr.addstr (c_y+5,c_x,'Press C to continue', curses. A_reverse) whileTrue:c=Stdscr.getch ()ifc = = Ord ('C')orc = = Ord ('C'): BreakZh_='1234567890-qwertyuiopasdfghjklzxcvbnm,[;l,]/[email protected]#$%^&* () _+} "? {: ><} ""';' whileTrue: forLineiinchRange (1,width-1): forLinejinchRange (1,height-1): ifLinej = =c_y:ifLinei <= 5orLinei+6 >=width:stdscr.addstr (Linej,linei,'$') Else: Stdscr.addstr (Linej,c_x,time.strftime ('%y-%m-%d%h:%m:%s'), curses. A_bold)Else: Randominx= Random.randint (0,len (zh_)-1) Stdscr.addstr (Linej,linei,zh_[randominx]) stdscr.move (c_y,c_x) Stdscr.refresh () Time.sleep ( /c4>1) Curses.endwin ()
In this way, a multi-line refresh demo is completed.
Python Console single-row refresh, multiline refresh