Hướng dẫn how do i change the color of my text console in python? - làm cách nào để thay đổi màu của bảng điều khiển văn bản trong python?

Dựa trên câu trả lời của Joeld, sử dụng https://pypi.python.org/pypi/lazyme pip install -U lazyme:
pip install -U lazyme:

from lazyme.string import color_print
>>> color_print('abc')
abc
>>> color_print('abc', color='pink')
abc
>>> color_print('abc', color='red')
abc
>>> color_print('abc', color='yellow')
abc
>>> color_print('abc', color='green')
abc
>>> color_print('abc', color='blue', underline=True)
abc
>>> color_print('abc', color='blue', underline=True, bold=True)
abc
>>> color_print('abc', color='pink', underline=True, bold=True)
abc

Screenshot:

Hướng dẫn how do i change the color of my text console in python? - làm cách nào để thay đổi màu của bảng điều khiển văn bản trong python?


Một số cập nhật cho color_print với các định dạng mới, ví dụ:

>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']

Lưu ý:

>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
0,
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
1 và
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
2 có thể không hoạt động trên tất cả các thiết bị đầu cuối và chúng không hoạt động trên Mac và Ubuntu.

E.g.,

>>> color_print('foo bar', color='pink', highlight='white')
foo bar
>>> color_print('foo bar', color='pink', highlight='white', reverse=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', bold=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', faint=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', faint=True, reverse=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', underline=True, reverse=True)
foo bar

Screenshot:

Hướng dẫn how do i change the color of my text console in python? - làm cách nào để thay đổi màu của bảng điều khiển văn bản trong python?

Trong bài viết này, chúng tôi sẽ học cách in văn bản màu bằng Python. Chúng tôi sẽ sử dụng một số mô-đun và thư viện tích hợp và một số mã tùy chỉnh là tốt. Trước tiên chúng ta hãy có một cái nhìn nhanh về cách Python đại diện cho mã màu.

Trong ngôn ngữ lập trình Python, văn bản có thể được biểu diễn bằng các màu khác nhau. Có rất đơn giản để sử dụng các thư viện Python cho màu sắc và định dạng trong thiết bị đầu cuối. Các lập trình viên nhận được phản hồi tốt hơn bằng cách in các văn bản màu.

Hãy xem một số ví dụ hữu ích để tô màu văn bản trong Python.

In văn bản màu bằng mô -đun Colorma

Chúng ta có thể sử dụng mô-đun

>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
3 tích hợp của Python để in văn bản đầy màu sắc. Nó là một mô-đun in đa nền tảng. Trong đó, văn bản màu có thể được thực hiện bằng cách sử dụng cách viết tắt hằng số
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
4 cho các chuỗi thoát
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
5. Chỉ cần nhập từ mô -đun Coloroma và nhận đầu ra mong muốn của bạn.

import colorama
from colorama import Fore

print(Fore.RED + 'This text is red in color')


Văn bản này có màu đỏ

In văn bản màu sử dụng mô -đun termcolor

Chúng ta có thể sử dụng mô-đun TermColor tích hợp để in màu văn bản bằng Python. TermColor là một mô -đun Python cho định dạng màu ANSII cho đầu ra trong thiết bị đầu cuối. Đối với điều này,

>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
6Module phải được nhập trước và sau đó được tô màu từ mô -đun TermColor như được đưa ra dưới đây.

import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)


Chào thế giới!

In văn bản màu bằng mã ANSI trong Python

Chúng tôi có thể sử dụng kiểu mã ANSI để làm cho văn bản của bạn dễ đọc và sáng tạo hơn, bạn có thể sử dụng mã Escape ANSI để thay đổi màu của đầu ra văn bản trong chương trình Python. Một trường hợp sử dụng tốt cho điều này là để làm nổi bật các lỗi. Các mã thoát được nhập ngay vào câu lệnh in.

print("\033[1;32m This text is Bright Green  \n")


Văn bản này có màu xanh lá cây tươi sáng

Mã thoát ANSI trên sẽ đặt màu văn bản thành màu xanh lá cây sáng. Định dạng là;

  • \ 033 [= mã thoát, điều này luôn giống nhau
  • 1 = phong cách, 1 cho bình thường.
  • 32 = Màu văn bản, 32 cho màu xanh lá cây tươi sáng.

In văn bản màu bằng mô -đun màucolored module

Chúng ta có thể sử dụng mô -đun màu và các chức năng của nó để tô màu văn bản trong Python. Nó là một thư viện có thể được sử dụng sau khi cài đặt bằng cách sử dụng lệnh PIP. Vì vậy, trước tiên, cài đặt nó và sau đó nhập nó vào tập lệnh Python của bạn để làm nổi bật các màu văn bản.

from colored import fg
print ('%s Hello World !!! %s' % (fg(1), attr(0)))


Chào thế giới !!!

Ví dụ 2

Chúng ta cũng có thể chuyển tên của màu vào hàm fg (). Hãy xem, nó in văn bản bằng màu xanh khi chúng ta vượt qua màu xanh như giá trị.

from colored import fg
color = fg('blue')
print (color + 'Hello World !!!')


Chào thế giới !!!

Đây là những cách khác nhau mà bạn có thể in văn bản của mình bằng các màu khác nhau. Bạn cũng có thể thêm các kiểu khác nhau vào văn bản của bạn, màu nền khác nhau cho văn bản của bạn.

Sự kết luận

Trong bài viết này, chúng tôi đã học cách tô màu văn bản và in nền màu bằng cách sử dụng một số hàm tích hợp như mô-đun

>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
7, mô-đun
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
8,
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter,
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']
9, v.v. Chúng tôi cũng đã sử dụng một số mã tùy chỉnh. Ví dụ, chúng tôi đã sử dụng các màu và văn bản khác nhau để làm nổi bật và in văn bản màu.