Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

python - 如何在Python的终端中打印彩色文本?(How to print colored text in terminal in Python?)

How can I output colored text to the terminal, in Python?

(如何在Python中将彩色文本输出到终端?)

What is the best Unicode symbol to represent a solid block?

(代表实体块的最佳Unicode符号是什么?)

  ask by aboSamoor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This somewhat depends on what platform you are on.

(这在某种程度上取决于您所使用的平台。)

The most common way to do this is by printing ANSI escape sequences.

(最常见的方法是打印ANSI转义序列。)

For a simple example, here's some python code from the blender build scripts :

(对于一个简单的示例,这是Blender构建脚本中的一些python代码:)

class bcolors:
    HEADER = '33[95m'
    OKBLUE = '33[94m'
    OKGREEN = '33[92m'
    WARNING = '33[93m'
    FAIL = '33[91m'
    ENDC = '33[0m'
    BOLD = '33[1m'
    UNDERLINE = '33[4m'

To use code like this, you can do something like

(要使用这样的代码,您可以执行以下操作)

print bcolors.WARNING + "Warning: No active frommets remain. Continue?" 
      + bcolors.ENDC

This will work on unixes including OS X, linux and windows (provided you use ANSICON , or in Windows 10 provided you enable VT100 emulation ).

(这将在包括OS X,Linux和Windows的Unix上运行(前提是您使用ANSICON ,或者在Windows 10中启用VT100仿真 )。)

There are ansi codes for setting the color, moving the cursor, and more.

(有用于设置颜色,移动光标等的ansi代码。)

If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the "curses" module, which handles a lot of the complicated parts of this for you.

(如果您对此感到复杂(听起来就像是在编写游戏),则应该查看“ curses”模块,该模块为您处理了许多复杂的部分。)

The Python Curses HowTO is a good introduction.

(Python Curses HowTO是一个很好的介绍。)

If you are not using extended ASCII (ie not on a PC), you are stuck with the ascii characters below 127, and '#' or '@' is probably your best bet for a block.

(如果您没有使用扩展的ASCII码(即不在PC上),那么您将只能使用127以下的ASCII字符,并且“#”或“ @”可能是最好的选择。)

If you can ensure your terminal is using a IBM extended ascii character set , you have many more options.

(如果可以确保您的终端使用的是IBM 扩展的ascii字符集 ,那么您还有更多选择。)

Characters 176, 177, 178 and 219 are the "block characters".

(字符176、177、178和219是“块字符”。)

Some modern text-based programs, such as "Dwarf Fortress", emulate text mode in a graphical mode, and use images of the classic PC font.

(一些现代的基于文本的程序,例如“矮人要塞”,以图形模式模拟文本模式,并使用经典PC字体的图像。)

You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see ( user-made tilesets ).

(您可以在Dwarf Fortress Wiki see( 用户制作的tileset )上找到一些可以使用的位图。)

The Text Mode Demo Contest has more resources for doing graphics in text mode.

(文本模式演示比赛有更多资源可以在文本模式下制作图形。)

Hmm.. I think got a little carried away on this answer.

(嗯..我认为这个答案有点过头了。)

I am in the midst of planning an epic text-based adventure game, though.

(不过,我正在计划一个史诗般的基于文本的冒险游戏。)

Good luck with your colored text!

(祝您彩色文字好运!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...