Code

完整名称: manim.mobject.text.code\_mobject.Code

class Code(code_file=None, code_string=None, language=None, formatter_style='vim', tab_width=4, add_line_numbers=True, line_numbers_from=1, background='rectangle', background_config=None, paragraph_config=None)[source]

基类: VMobject

高亮显示源代码列表。

示例

常规用法

listing = Code(
    "helloworldcpp.cpp",
    tab_width=4,
    formatter_style="emacs",
    background="window",
    language="cpp",
    background_config={"stroke_color": WHITE},
    paragraph_config={"font": "Noto Sans Mono"},
)

我们也可以渲染作为字符串传递的代码。由于自动语言检测可能不太稳定,建议明确指定语言

示例: CodeFromString

../_images/CodeFromString-1.png
from manim import *

class CodeFromString(Scene):
    def construct(self):
        code = '''from manim import Scene, Square

class FadeInSquare(Scene):
    def construct(self):
        s = Square()
        self.play(FadeIn(s))
        self.play(s.animate.scale(2))
        self.wait()'''

        rendered_code = Code(
            code_string=code,
            language="python",
            background="window",
            background_config={"stroke_color": "maroon"},
        )
        self.add(rendered_code)
class CodeFromString(Scene):
    def construct(self):
        code = '''from manim import Scene, Square

class FadeInSquare(Scene):
    def construct(self):
        s = Square()
        self.play(FadeIn(s))
        self.play(s.animate.scale(2))
        self.wait()'''

        rendered_code = Code(
            code_string=code,
            language="python",
            background="window",
            background_config={"stroke_color": "maroon"},
        )
        self.add(rendered_code)

参数:
  • code_file (StrPath | None) – 要显示的代码文件路径。

  • code_string (str | None) – 或者,要显示的代码字符串。

  • language (str | None) – 代码的编程语言。如果未指定,将根据文件扩展名或代码本身进行猜测。

  • formatter_style (str) – 用于代码高亮的样式。默认为 "vim"。所有可用样式的列表可以通过调用 Code.get_styles_list() 获得。

  • tab_width (int) – Tab 字符的宽度(以空格计)。默认为 4。

  • add_line_numbers (bool) – 是否显示行号。默认为 True

  • line_numbers_from (int) – 要显示的第一行行号。默认为 1。

  • background (Literal['rectangle', 'window']) – 要使用的背景类型。可以是 "rectangle"(默认)或 "window"

  • background_config (dict[str, Any] | None) – 传递给背景构造函数的关键字参数。默认设置存储在类属性 default_background_config 中(也可以直接修改)。

  • paragraph_config (dict[str, Any] | None) – 传递给包含代码和行号的 Paragraph 对象构造函数的关键字参数。默认设置存储在类属性 default_paragraph_config 中(也可以直接修改)。

方法

get_styles_list

获取所有可用格式化器样式的列表。

属性

animate (动画)

用于动画化 self 的任何方法的应用。

animation_overrides (动画覆盖)

颜色

default_background_config

default_paragraph_config

depth (深度)

mobject 的深度。

fill_color (填充颜色)

如果存在多种颜色(用于渐变),则返回第一种颜色

height (高度)

mobject 的高度。

n_points_per_curve (每条曲线的点数)

sheen_factor (光泽因子)

stroke_color (描边颜色)

width (宽度)

mobject 的宽度。

_original__init__(code_file=None, code_string=None, language=None, formatter_style='vim', tab_width=4, add_line_numbers=True, line_numbers_from=1, background='rectangle', background_config=None, paragraph_config=None)

初始化自身。有关准确签名,请参阅 help(type(self))。

参数:
  • code_file (StrPath | None)

  • code_string (str | None)

  • language (str | None)

  • formatter_style (str)

  • tab_width (int)

  • add_line_numbers (bool)

  • line_numbers_from (int)

  • background (Literal['rectangle', 'window'])

  • background_config (dict[str, Any] | None)

  • paragraph_config (dict[str, Any] | None)

classmethod get_styles_list()[source]

获取所有可用格式化器样式的列表。

返回类型:

list[str]