VDict¶
限定名称: manim.mobject.types.vectorized\_mobject.VDict
- class VDict(mapping_or_iterable={}, show_keys=False, **kwargs)[source]¶
基础类:
VMobject一个类似于VGroup的类,也提供通过键访问子对象的功能,就像python字典一样
- Parameters:
- show_keys¶
是否也显示与mobject关联的键。这在调试时可能很有用,特别是当
VDict中有很多mobject时。显示时,键位于mobject的左侧。默认为False。- Type:
bool
- submob_dict¶
是实际用于将键绑定到mobjects的Python字典。
- Type:
dict
示例
示例:ShapesWithVDict ¶
from manim import * class ShapesWithVDict(Scene): def construct(self): square = Square().set_color(RED) circle = Circle().set_color(YELLOW).next_to(square, UP) # create dict from list of tuples each having key-mobject pair pairs = [("s", square), ("c", circle)] my_dict = VDict(pairs, show_keys=True) # display it just like a VGroup self.play(Create(my_dict)) self.wait() text = Tex("Some text").set_color(GREEN).next_to(square, DOWN) # add a key-value pair by wrapping it in a single-element list of tuple # after attrs branch is merged, it will be easier like `.add(t=text)` my_dict.add([("t", text)]) self.wait() rect = Rectangle().next_to(text, DOWN) # can also do key assignment like a python dict my_dict["r"] = rect # access submobjects like a python dict my_dict["t"].set_color(PURPLE) self.play(my_dict["t"].animate.scale(3)) self.wait() # also supports python dict styled reassignment my_dict["t"] = Tex("Some other text").set_color(BLUE) self.wait() # remove submobject by key my_dict.remove("t") self.wait() self.play(Uncreate(my_dict["s"])) self.wait() self.play(FadeOut(my_dict["c"])) self.wait() self.play(FadeOut(my_dict["r"], shift=DOWN)) self.wait() # you can also make a VDict from an existing dict of mobjects plain_dict = { 1: Integer(1).shift(DOWN), 2: Integer(2).shift(2 * DOWN), 3: Integer(3).shift(3 * DOWN), } vdict_from_plain_dict = VDict(plain_dict) vdict_from_plain_dict.shift(1.5 * (UP + LEFT)) self.play(Create(vdict_from_plain_dict)) # you can even use zip vdict_using_zip = VDict(zip(["s", "c", "r"], [Square(), Circle(), Rectangle()])) vdict_using_zip.shift(1.5 * RIGHT) self.play(Create(vdict_using_zip)) self.wait()
class ShapesWithVDict(Scene): def construct(self): square = Square().set_color(RED) circle = Circle().set_color(YELLOW).next_to(square, UP) # create dict from list of tuples each having key-mobject pair pairs = [("s", square), ("c", circle)] my_dict = VDict(pairs, show_keys=True) # display it just like a VGroup self.play(Create(my_dict)) self.wait() text = Tex("Some text").set_color(GREEN).next_to(square, DOWN) # add a key-value pair by wrapping it in a single-element list of tuple # after attrs branch is merged, it will be easier like `.add(t=text)` my_dict.add([("t", text)]) self.wait() rect = Rectangle().next_to(text, DOWN) # can also do key assignment like a python dict my_dict["r"] = rect # access submobjects like a python dict my_dict["t"].set_color(PURPLE) self.play(my_dict["t"].animate.scale(3)) self.wait() # also supports python dict styled reassignment my_dict["t"] = Tex("Some other text").set_color(BLUE) self.wait() # remove submobject by key my_dict.remove("t") self.wait() self.play(Uncreate(my_dict["s"])) self.wait() self.play(FadeOut(my_dict["c"])) self.wait() self.play(FadeOut(my_dict["r"], shift=DOWN)) self.wait() # you can also make a VDict from an existing dict of mobjects plain_dict = { 1: Integer(1).shift(DOWN), 2: Integer(2).shift(2 * DOWN), 3: Integer(3).shift(3 * DOWN), } vdict_from_plain_dict = VDict(plain_dict) vdict_from_plain_dict.shift(1.5 * (UP + LEFT)) self.play(Create(vdict_from_plain_dict)) # you can even use zip vdict_using_zip = VDict(zip(["s", "c", "r"], [Square(), Circle(), Rectangle()])) vdict_using_zip.shift(1.5 * RIGHT) self.play(Create(vdict_using_zip)) self.wait()方法
将键值对添加到
VDict对象中。一个实用函数,由
add()使用,用于将键值对添加到submob_dict中。获取与特定
VDict对象关联的所有子对象从具有键key的
VDict对象中移除mobject属性
animate用于动画化
self的任何方法的应用。animation_overridescolordepthmobject的深度。
fill_color如果有多种颜色(用于渐变),则返回第一个颜色
heightmobject的高度。
n_points_per_curvesheen_factorstroke_colorwidthmobject的宽度。
- _original__init__(mapping_or_iterable={}, show_keys=False, **kwargs)¶
初始化自身。有关准确的签名,请参阅 help(type(self))。
- add(mapping_or_iterable)[source]¶
将键值对添加到
VDict对象中。此外,它在内部将值添加到
Mobject的submobjectslist中,该列表负责实际的屏幕显示。- Parameters:
mapping_or_iterable (Mapping[Hashable, VMobject] | Iterable[tuple[Hashable, VMobject]]) – 指定键和mobjects的键值映射的参数。
- Returns:
返回调用此方法的
VDict对象。- Return type:
示例
正常用法:
square_obj = Square() my_dict.add([('s', square_obj)])
- add_key_value_pair(key, value)[source]¶
一个由
add()使用的实用函数,用于将键值对添加到submob_dict。实际上并不打算在外部使用。- Parameters:
key (Hashable) – 要添加的子对象的键。
value (VMobject) – 与键关联的mobject
- Return type:
无
- Raises:
TypeError – 如果该值不是 VMobject 的实例
示例
正常用法:
square_obj = Square() self.add_key_value_pair('s', square_obj)