Example of Python Control Terminal Outputting Text
- 2021-07-16 02:45:16
- OfStack
As shown below:
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
To use code like this, you can do something like
print bcolors.WARNING + "Warning: No active frommets remain. Continue?"
+ bcolors.ENDC
The following is the corresponding color table:
Format: \033[ Display mode ; Foreground color ; Background color m
Description:
Foreground color Background color Color
---------------------------------------
30 40 Black
31 41 Red
32 42 Green
33 43 Color
34 44 Blue
35 45 Purplish red
36 46 Cyan blue
37 47 White
Display mode Meaning
-------------------------
0 Terminal default settings
1 Highlight
4 Use underlines
5 Flicker
7 Inverted white display
8 Invisible
Examples:
\033[1;31;40m <!--1- Highlight 31- Foreground color red 40- Background color black -->
\033[0m <!-- Adopt the terminal default setting, that is, cancel the color setting -->