Canvas¶
Module: kivy.graphics.instructions
Added in 1.0.0
The Canvas is the root object used for drawing by a Widget. Check the class documentation for more information about the usage of Canvas.
画布是一个 基类对象 被用来以一个组件的方式画画。 检查 类文档来获取canvas画画使用的更多信息
APIHide Description ⇑
class kivy.graphics.instructions.Callback(callback=None, **kwargs)¶
Bases: kivy.graphics.instructions.Instruction
A Callback is an instruction that will be called when the drawing operation is performed. When adding instructions to a canvas, you can do this:
一个回收信号,这是一个将当画画操作被执行时一个回收信号被召唤用法说明。 当添加用法说明到一个画布时, 你可以这么做:
with self.canvas: Color(1, 1, 1) Rectangle(pos=self.pos, size=self.size) Callback(self.my_callback)
The definition of the callback must be:
def my_callback(self, instr): print('I have been called!')
Warning
Note that if you perform many and/or costly calls to callbacks, you might potentially slow down the rendering performance significantly.
注意 如果你执行许多 并行 /或者单行 占运行的召唤到回收信号里, 你可能潜在地降低了渲染执行效率。
The updating of your canvas does not occur until something new happens. From your callback, you can ask for an update:
更新你的画布 并不出现 知道某个玩意新发生时。 从你的回调里你可以寻求一个更新:
with self.canvas: self.cb = Callback(self.my_callback) # then later in the code self.cb.ask_update()
If you use the Callback class to call rendering methods of another toolkit, you will have issues with the OpenGL context. The OpenGL state may have been manipulated by the other toolkit, and as soon as program flow returns to Kivy, it will just break. You can have glitches, crashes, black holes might occur, etc. To avoid that, you can activate the reset_context option. It will reset the OpenGL context state to make Kivy’s rendering correct after the call to your callback.
如果你使用回调类来召唤渲染其他的工具箱方法, 你将有些问题伴随着OpenGL 内容。 OpenGL 的状态可以被其他的工具包操控, 并且项目一传播信号返回给Kivy,就会break。 你可以有一些小故障、猛撞、黑洞可能会出现等等。 为了避免那些, 你可以激活reset_context 选项。 它将设置重新设置OpenGL 内容状态来 确保Kivy渲染正确地在召唤你地回调信号之后。
Warning
The reset_context is not a full OpenGL reset. If you have issues regarding that, please contact us.
reset_context 不是一个完整的OpenGL 重新设定。 如果你有一些问题关于那, 请联系我们。
ask_update(self)¶
Inform the parent canvas that we’d like it to update on the next frame. This is useful when you need to trigger a redraw due to some value having changed for example.
通知父类画布我们想来更新下副画。 这是有用的当你需要引用一个重新绘画因为一些值被改变了。
New in version 1.0.4.
callback¶
Property for getting/setting func.
属性来获取/设定 功能。
reset_context¶
Set this to True if you want to reset the OpenGL context for Kivy after the callback has been called.
设置这到True 如果你想来重新设定OpenGL 内容为了Kivy在回调信号被接收后。
class kivy.graphics.instructions.Canvas(**kwargs)¶
Bases: kivy.graphics.instructions.CanvasBase
instructions that you want to be used for drawing.
你想用来画画的用法说明
Note
The Canvas supports Python’s with
statement and its enter & exit semantics.
画布 支持python 同状态和它 enter 和exit 语义。
Usage of a canvas without the with
statement:
没有with声明使用一个画布。
self.canvas.add(Color(1., 1., 0)) self.canvas.add(Rectangle(size=(50, 50)))
Usage of a canvas with Python’s with
statement:
使用python with 声明的一个canvas使用。
with self.canvas: Color(1., 1., 0) Rectangle(size=(50, 50))
add(self, Instruction c)¶
Append an Instruction to our list. If the canvas contains an after group, then this instruction is inserted just before the after group, which remains last. This is different from how insert()
works, which can insert anywhere.
增加一个用法说明到我们的列表。 如果画布包含着一个after 组, 然后这个用法说明是被添加到after组的前面, after组保持在末尾。 这是不同的和如何使用insert()工作,insert()可以插入到任何地方。
after¶
Property for getting the ‘after’ group. 获取'after'组的属性
ask_update(self)¶
Inform the canvas that we’d like it to update on the next frame. This is useful when you need to trigger a redraw due to some value having changed for example.
赋予画布我们想来更新在下一个画面的特征。 这是有用的当你需要引用一个重新绘画, 例如 因为一些值被改变。
before¶
Property for getting the ‘before’ group.
clear(self)¶
Clears every Instruction in the canvas, leaving it clean.
清除画布中的每个用法说明, 让它清空。
draw(self)¶
Apply the instruction to our window. 使用用法说明到我们的窗口。
has_after¶
Property to see if the after group has already been created. 属性来看看是否after组已经被创造。
New in version 1.7.0.
has_before¶
Property to see if the before group has already been created.属性来看看是否before组一斤半给创造
New in version 1.7.0.
opacity¶
Property to get/set the opacity value of the canvas. 属性来 获取/设置 画布的透明度值。
New in version 1.4.1.
The opacity attribute controls the opacity of the canvas and its children. Be careful, it’s a cumulative attribute: the value is multiplied to the current global opacity and the result is applied to the current context color.
透明度属性管控着画布和它子类的透明度。 请注意,它是一个累积属性: 值是乘以当前全局透明度, 并且结果是被应用到当前内容颜色。
For example: if your parent has an opacity of 0.5 and a child has an opacity of 0.2, the real opacity of the child will be 0.5 * 0.2 = 0.1.
举个例: 如果你的父类有一个透明度是0.5, 并且一个子类有一个透明度0.2, 真正的子类透明度将是 0.5 * 0.2 = 0.1
Then, the opacity is applied on the shader as:
然后,透明度是被应用的在着色器上像下面一样:
frag_color = color * vec4(1.0, 1.0, 1.0, opacity);
remove(self, Instruction c)¶
class kivy.graphics.instructions.CanvasBase¶
Bases: kivy.graphics.instructions.InstructionGroup
CanvasBase provides the context manager methods for the Canvas.
画布基类 提供管理画布方法的内容
class kivy.graphics.instructions.ContextInstruction(**kwargs)¶
Bases: kivy.graphics.instructions.Instruction
that don’t have a direct visual representation, but instead modify the current Canvas’ state, e.g. texture binding, setting color parameters, matrix manipulation and so on.
那没有一个直接的视觉的代表, 但是被替代修改当前画布的状态, 例如, 纹理绑定, 设置 颜色元素, 矩阵操作 等等。
class kivy.graphics.instructions.Instruction(**kwargs)¶
Bases: kivy.event.ObjectWithUid
usage only, don’t use it directly. 用法单一, 不要直接使用它。
flag_data_update(self)¶
flag_update(self, int do_parent=1)¶
group¶
group: unicode
proxy_ref¶
Return a proxy reference to the Instruction i.e. without creating a reference of the widget. See weakref.proxy for more information.
返回一个代理的引文到用法说明。 不创造一个组件的用法说明。
New in version 1.7.2.
class kivy.graphics.instructions.InstructionGroup(**kwargs)¶
Bases: kivy.graphics.instructions.Instruction
of graphics instructions. It can be used directly as follows:
图形用法说明, 它可以被直接像下面使用:
blue = InstructionGroup()
blue.add(Color(0, 0, 1, 0.2))
blue.add(Rectangle(pos=self.pos, size=(100, 100)))
green = InstructionGroup()
green.add(Color(0, 1, 0, 0.4))
green.add(Rectangle(pos=(100, 100), size=(100, 100)))
# Here, self should be a Widget or subclass [self.canvas.add(group) for group in [blue, green]]
add(self, Instruction c)¶
Add a new Instruction to our list.
children¶
children: list
clear(self)¶
Remove all the Instructions.
get_group(self, unicode groupname)¶
Return an iterable for all the Instructions with a specific group name.
indexof(self, Instruction c)¶
insert(self, int index, Instruction c)¶
Insert a new Instruction into our list at index.
length(self)¶
remove(self, Instruction c)¶
Remove an existing Instruction from our list.
remove_group(self, unicode groupname)¶
Remove all Instructions with a specific group name.
class kivy.graphics.instructions.RenderContext(*args, **kwargs)¶
Bases: kivy.graphics.instructions.Canvas
-
The vertex shader 顶点着色器
-
The fragment shader 片元着色器
-
The default texture 默认纹理
-
The state stack (color, texture, matrix…) 状态栈
shader¶ 着色器
Return the shader attached to the render context. 返回着色器隶属于渲染上下文
use_parent_frag_modelview¶
If True, the parent fragment modelview matrix will be used.
如果为True, 父类 片元 模式视图 matrix 将被使用。
New in version 1.10.1: rc = RenderContext(use_parent_frag_modelview=True)
use_parent_modelview¶
If True, the parent modelview matrix will be used.
如果为True, 父类 模式视图 matrix 将被使用
New in version 1.7.0.
Before:
rc['modelview_mat'] = Window.render_context['modelview_mat']
Now:
rc = RenderContext(use_parent_modelview=True)
use_parent_projection¶
If True, the parent projection matrix will be used. 如果为True, 父类投影图 matrix 将被使用。
New in version 1.7.0.
Before:
rc['projection_mat'] = Window.render_context['projection_mat']
Now:
rc = RenderContext(use_parent_projection=True)
class kivy.graphics.instructions.VertexInstruction(**kwargs)¶
Bases: kivy.graphics.instructions.Instruction
that have a direct visual representation on the canvas, such as Rectangles, Triangles, Lines, Ellipse and so on.
source¶
This property represents the filename to load the texture from. If you want to use an image as source, do it like this:
这属性代表着从哪加载纹理的文件夹名字。 如果你像使用一个照片作为一个源头,像这样做:
with self.canvas: Rectangle(source='mylogo.png', pos=self.pos, size=self.size)
Here’s the equivalent in Kivy language:
这是等同的在kv语言里
<MyWidget>: canvas: Rectangle: source: 'mylogo.png' pos: self.pos size: self.size
Note
The filename will be searched for using the kivy.resources.resource_find() function.
文件夹名字将被寻找来使用 kivy.resources.resource_find() 功能
tex_coords¶ 坐标
This property represents the texture coordinates used for drawing the vertex instruction. The value must be a list of 8 values.
这属性代表着 纹理坐标 被用来画 顶点指令。 值必须是一个有8个值的列表。
A texture coordinate has a position (u, v), and a size (w, h). The size can be negative, and would represent the ‘flipped’ texture. By default, the tex_coords are:
一个 纹理坐标 有一个 位置(u,v) , 和一个大小(w,h). 大小可以是负数, 并且可以代表 反转的纹理。默认,tex_coords是:
[u, v, u + w, v, u + w, v + h, u, v + h]
You can pass your own texture coordinates if you want to achieve fancy effects.
你可以通过你自己个的 纹理坐标 如果你想成就 优质的效果。
Warning
The default values just mentioned can be negative. Depending on the image and label providers, the coordinates are flipped vertically because of the order in which the image is internally stored. Instead of flipping the image data, we are just flipping the texture coordinates to be faster.
默认值 是被提及的可以是负数。 取决于提供的照片 和 标签,坐标们是垂直翻转的因为图片顺序是内部存储的。 替代翻转 图片的数据, 我们只需翻转 纹理坐标 可以更快。
texture¶
Property that represents the texture used for drawing this Instruction. You can set a new texture like this:
属性 代表纹理来画这用法说明。 你可以设置一个新的纹理像这样:
from kivy.core.image import Image
当使用image控件时,可以通过其texture属性访问底层的Texture对象
texture = Image('logo.png').texture
with self.canvas:
Rectangle(texture=texture, pos=self.pos, size=self.size)
Usually, you will use the source attribute instead of the texture.
通常,你可以使用 source 属性代替纹理。
kivy 里的texture 有什么用?
在 Kivy 框架中,Texture
对象扮演着非常重要的角色,它允许开发者在 GPU 上高效地处理图像数据,并将其渲染到屏幕上。以下是 Texture
在 Kivy 中的主要用途:
-
图像渲染:
Texture
对象通常与图像数据相关联,这些图像数据可以来自文件、内存或其他数据源。通过将图像数据加载到Texture
中,Kivy 可以在 GPU 上对其进行处理,从而加速渲染过程。这意味着当你使用 Kivy 的Image
控件或其他与图像相关的控件时,实际上是在使用Texture
对象来显示图像。 -
自定义绘图:
除了直接用于显示图像外,Texture
对象还可以用于自定义绘图。通过在 Kivy 的Canvas
上使用绘图指令(如Rectangle
、Ellipse
等),并指定texture
参数,你可以将Texture
对象绘制到任何形状或路径上。这为创建复杂的图形和动画提供了强大的工具。 -
纹理映射:
在 3D 图形编程中,纹理映射是一种常见的技术,用于将图像(即纹理)应用到 3D 对象的表面上。虽然 Kivy 主要是一个 2D 框架,但它也支持一些基本的 3D 渲染功能。在这些情况下,Texture
对象可以被用作 3D 对象的纹理,从而为其添加更多的细节和视觉效果。 -
视频和动画:
由于Texture
对象可以动态更新,因此它们非常适合用于显示视频和动画。通过定期更新与Texture
对象关联的图像数据,你可以创建流畅的动画效果或播放视频文件。这在游戏开发、交互式应用程序和多媒体播放器等场景中非常有用。 -
内存和性能优化:
将图像数据加载到Texture
对象中并在 GPU 上进行处理可以显著提高渲染性能。与直接在 CPU 上处理图像数据相比,GPU 加速的渲染可以更快地完成,并且消耗更少的 CPU 资源。此外,通过有效地管理Texture
对象的生命周期和内存使用,你可以避免不必要的内存泄漏和性能下降。 -
与 OpenGL ES 集成:
Kivy 底层使用 OpenGL ES 作为其图形渲染引擎。Texture
对象为开发者提供了一个与 OpenGL ES 集成的接口,允许他们直接访问和操作 GPU 上的图像数据。这为那些需要更底层图形控制或希望与现有 OpenGL ES 代码集成的开发者提供了便利。
总之,Texture
对象在 Kivy 中扮演着至关重要的角色,它们为图像渲染、自定义绘图、纹理映射、视频和动画播放以及与 OpenGL ES 集成提供了强大的支持。通过有效地使用 Texture
对象,开发者可以创建出高性能、交互性强且视觉效果丰富的应用程序。