如何从kivy小部件画布中删除特定说明? [英] How can I remove specific instructions from kivy widget canvas?

查看:114
本文介绍了如何从kivy小部件画布中删除特定说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于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和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.

推荐答案

您可以:

  1. 通过执行canvas.remove()删除画布的子代,并传递所需的图形指令实例.您可以使用canvas.children进行迭代并获得它.

  1. 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 with canvas.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小部件画布中删除特定说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆