本文介绍了为什么小部件在 Kivy 中似乎共享同一个画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kivy 文档指定 "Kivy 中的每个小部件都已具有默认情况下他们的画布".然而,在实践中,小部件似乎持有对整个窗口的共享画布的引用,而不是自己拥有一个.

当我使用 RotateTranslateScalecontext_instructions 时,这一点变得更加清晰.如果我在特定的 WidgetRotate,它将影响后续的小部件.一切似乎都在旋转.在绘制其他任何内容之前,我必须取消 Rotate 画布.此外,它是在旋转的画布中还是在下一个画布中并不重要.Color 指令也是如此.

对这种默认行为施加和例外的唯一方法似乎是RelativeLayout.在那种情况下,我刚才所说的所有内容都与 RelativeLayout 相关,除了 Color 继续是全局的.有人可能会说 Color 不是定位指令,而 RelativeLayout 只是相对的定位.RotateTranslate 是位置指令,但它是 Scale 吗?

我实际上只是用一个具体的例子完成了一篇博文

我显然遗漏了一些东西,因为每个小部件都有自己的 canvas.所以,基本上我的问题是为什么小部件在 Kivy 中似乎共享同一个画布?

解决方案

我把自己逼进了一个大错误.这一切都是因为 canvas 这个名字令人困惑而开始的.我开始认为画布是我们可以在其中绘画的空间.但不是,这里的文档其实很清楚:

使用这个类添加你想用于绘图的图形或上下文指令Kivy API - 画布类)

这里:

画布是一个图形对象,其中包含小部件图形表示的所有绘图指令(Kivy API - 画布属性).

因此,画布是一组指令(如果我们考虑 canvas.beforecanvas.after,则为一组指令).指令不会在 canvas 上绘制或执行,指令只是添加到 canvas.指令在坐标空间中执行.例如,当画布中有一个Rotate指令时,它将被应用到任何VertexInstruction(例如一个Line),然后才显示在中坐标空间.

因此,小部件不共享 canvas,但我们添加到 canvas 的指令共享相同的坐标空间.p>

The Kivy documentation specifies that "each widget in Kivy already have by default their Canvas". However, in practice, it seems that the widgets hold a reference to a shared canvas of the whole window instead of having one just for themselves.

This becomes more clear when I use the context_instrucions of Rotate, Translate or Scale. If I Rotate inside a particular Widget, it will affect subsequent widgets. Everything seems to have rotated. I have to un-Rotate the canvas before drawing anything else. Moreover, it doesn't really matter if it is in the rotated canvas or in the next one. The same happens with the instruction Color.

The only way to impose and exception to this default behaviour seems to be the RelativeLayout. In that case, all what I just said became relative to the RelativeLayout except Color which continue being global. One can argue that Color is not a positioning instruction and RelativeLayout is relative just to position. Rotate and Translate are position instructions but is it Scale?

I actually just finish a blog post with an specific example

I am obviously missing something with the part of each Widget has its own canvas. So, basically my question is why does it seems that the widgets share the same canvas in Kivy?

解决方案

I drove myself into a big mistake. This all started because the name canvas is confusing. I started to think that a canvas was a space in which we can draw. But not, the documentation is actually very clear here:

And here:

So, a canvas is a set of instructions (or sets if we think about canvas.before and canvas.after). The instructions don't draw or are executed on a canvas, the instructions are just added to a canvas. The instructions are executed in the coordinate space. For example, when there is a Rotate instruction in the canvas, it is going to be applied to any VertexInstruction (a Line for example) before being displayed in the coordinate space.

So, the widgets don't share a canvas but the instructions we add to the canvas share the same coordinate space.

这篇关于为什么小部件在 Kivy 中似乎共享同一个画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:31