安慰#

介绍#

Use print to display text in the Console in VEXcode V5 or VS Code.

所有新项目都从光标位于第 1 行开始。

By using print, you can display values to the Console.

Along with normal text and values, print can output special character sequences (escape codes) that change font color or clear the Console.

注意: 使用 VEXcode 控制台时,必须先连接到控制台串口,然后才能将任何输出打印到控制台。

以下是可用方法列表:

操作 – 将文本打印到控制台并使用转义序列进行格式化。

  • print – Prints text to the Console.

变异器 – 更改控制台输出文本的颜色。

行动#

print#

print displays values on the Console.

Usage:
print(text, end)

参数

描述

text

要打印到控制台的字符串(用引号括起来)或数字。

end

Optional. By default, Python will add a new line after each print command. To print multiple values to the same line, use the end parameter.

# Print a greeting
print("Hello there!")

# Print text on the same line
print("This text will be ", end="")
print("printed on the same line")

转义序列#

The Console supports escape sequences that can be output via print to change colors or clear the Console.

更改字体颜色#

To change the font color, you can print a specific Escape Sequence or use the set_console_text_color method.

您可以使用以下颜色代码更改打印到控制台时字体的颜色:

  • [31m - Red

  • [32m - Green

  • [34m - Blue

  • [30m - Black

  • [37m - White

  • [33m - Yellow

  • [91m - Orange

  • [35m - Purple

  • [36m - Cyan

  • [97m - Transparent

All color codes need to follow an escape sequence = \033 in order to function.

You can either use this directly with print as a string:

# Print VEXcode in Red to the Console
print("\033[31mVEXcode")

Or, you can use it on its own to set all uses of print afterwards to the set color:

# Set the Console text color to Red before printing
print("\033[31m")
print("VEXcode")

清除控制台#

By using \033[2J inside of print, you can clear the entire Console. This will also set the cursor back to row 1.

# Clear the Console after 2 seconds
print("VEXcode")
wait(2, SECONDS)
print("\033[2J")

变异体#

set_console_text_color#

set_console_text_color sets the Console text color for subsequent calls to print.

用法:

set_console_text_color(color)

参数

描述

color

The color to set:

  • Color.Black
  • Color.BLUE
  • Color.CYAN
  • Color.GREEN
  • Color.ORANGE
  • Color.PURPLE
  • Color.RED
  • Color.TRANSPARENT
  • Color.WHITE
  • Color.YELLOW

# Set the console text color to blue, then print
set_console_text_color(Color.BLUE)
print("This text will be blue")

You can also use escape sequences with print to change the text color.