KIVY Canvas¶

Canvas

Jump to API

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.

Usage of a canvas without the with statement:

复制代码
self.canvas.add(Color(1., 1., 0))
self.canvas.add(Rectangle(size=(50, 50)))

Usage of a canvas with Python's with statement:

复制代码
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

Property for getting the 'after' group.

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.

New in version 1.7.0.

has_before

Property to see if the before group has already been created.

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.

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.

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.

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.

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:

复制代码
<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.

tex_coords

This property represents the texture coordinates used for drawing the vertex instruction. The value must be a list of 8 values.

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, 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

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.

相关推荐
SelectDB5 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码13 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵1 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li1 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸1 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学1 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田2 天前
Pydantic校验配置文件
python
hboot2 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi2 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi2 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab