How to remove specific instructions from the canvas of a kiwi widget?

I am writing a kivy-based graphics application where I can dynamically add nodes to the drawing area and then connect them with directional lines (= edge). Nodes can be dragged inside the drawing area, and the edges should always be in touch with them.

The Edge class is defined from the Widget class. Its graphic representation consists of two parts: the line itself (= Line) and the tip of the line (= triangle). When the Edge widget is drawn, its canvas is first translated and rotated, after which the tip of the edge is drawn on the canvas. After that, the canvas is rotated back to its original position, and part of the line is drawn on the canvas.

When the nodes are moved, the canvas of the Edge widget is cleared and the graphic representation is drawn again. However, this does not work, as I planned.

If I use self.canvas.clear () at the beginning of the draw method for the Edge class, the old line is not deleted, and I get a few lines on the canvas.

If I use self.canvas.after.clear (), I get a completely confused view of my drawing area, since the clear method also removes the PopMatrix statement from canvas.after.

How can I just remove the graphic representation of a string from canvas.after? Is there a better way to do this?

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 

UPDATE I changed my approach, and now I draw everything on canvas, and not draw on canvas and canvas. After. Now I got the result that I wanted, but if someone knows how to delete individual canvas instructions, it would be nice to find out.

+6
source share
1 answer

You can:

  • remove the children of the canvas by executing canvas.remove() and pass in an instance of the required graphic instructions. You can iterate and get it with canvas.children .

  • assign group to the canvas property, then use canvas.remove_group() . This is the best way to classify and delete many graphic instructions without storing links to them.

+6
source

All Articles