问题描述
我正在编写一个基于 kivy 的图形应用程序,我可以在其中动态地将节点添加到绘图区域,然后将它们与有向线 (=edge) 连接起来.节点可以在绘图区域内拖动,边缘应始终与它们保持连接.
I’m writing a kivy based graph app where I can dynamically add nodes to a draw area and then connect them with directed lines(=edge). Nodes can be dragged inside the draw area and edges should stay connected to them always.
Edge 类派生自 Widget 类.它的图形表示由两部分组成,线本身(=线)和线的尖端(=三角形).当一个 Edge 小部件被绘制时,它的画布首先被平移和旋转,然后边缘的尖端被绘制到画布上.之后,画布被旋转回原来的位置,线条部分被绘制到canvas.after.
Edge class is derived from Widget class. Its graphical presentation consists of two parts, the line itself(=Line) and the tip of the line(=Triangle). When an Edge widget is drawn, its canvas is at first translated and rotated after which the tip of the edge is drawn to the canvas. After that, the canvas is rotated back to its original position and the line part is drawn to canvas.after.
当节点移动时,Edge 小部件的画布被清除并再次绘制图形表示.然而,这并没有按我的计划工作.
When nodes are moved, the canvas of the Edge widget is cleared and the graphical presentation is drawn again. However, this doesn’t work as I planned.
如果我在 Edge 类的 draw 方法的开头使用 self.canvas.clear() ,旧线不会被删除,我会在画布上得到多条线.
If I use self.canvas.clear() at the beginning of the draw method of the Edge class, the old line is not removed and I get multiple lines on the canvas.
如果我使用 self.canvas.after.clear(),我的绘图区域会完全混乱,因为 clear 方法还会从 canvas.after 中删除 PopMatrix 指令.
If I use self.canvas.after.clear() I get a completely messed up view of my draw area, since the clear method also removes the PopMatrix instruction from the canvas.after.
我怎样才能从 canvas.after 中删除线条的图形表示?有更好的方法吗?
我的 Edge 类的 Kivy 文件:
Kivy file of my Edge class:
<Edge>:
id: ed
size_hint: None, None
canvas.before:
Color:
rgb: 0.9, 0.1, 0.1
PushMatrix
Translate:
x: ed.translate_x
y: ed.translate_y
Rotate:
angle: ed.rot_angle
origin: ed.rot_origin_x, ed.rot_origin_y
canvas.after:
PopMatrix
更新我改变了我的方法,现在我在画布上绘制所有内容,而不是在画布和 canvas.after 上绘制.现在我得到了我想要的结果,但如果有人知道如何删除单独的画布指令,我会很高兴知道.
UPDATE I changed my approach and now I draw everything on the canvas instead of drawing to canvas and canvas.after. Now I got the result that I wanted, but still if someone knows how to remove the individual canvas instructions it would be nice to know.
推荐答案
你可以:
通过执行
canvas.remove()
删除画布的子项,并传递所需的图形指令实例.您可以使用canvas.children
进行迭代并获得一个.
remove a children of a canvas by doing
canvas.remove()
and pass the instance of the graphics instructions you want. You can iterate and get the one withcanvas.children
.
将 group
名称分配给画布属性,然后使用 canvas.remove_group()
.这是分类和删除大量图形指令而不保留对它们的引用的最佳方法.
assign a group
name to a canvas property, then use canvas.remove_group()
. This is the best way to categorize and remove lot of graphics instructions without keeping a reference to them.
这篇关于如何从 kivy 小部件画布中删除特定说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!