問題描述
我正在編寫一個(gè)基于 kivy 的圖形應(yīng)用程序,我可以在其中動(dòng)態(tài)地將節(jié)點(diǎn)添加到繪圖區(qū)域,然后將它們與有向線 (=edge) 連接起來.節(jié)點(diǎn)可以在繪圖區(qū)域內(nèi)拖動(dòng),邊緣應(yīng)始終與它們保持連接.
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 類.它的圖形表示由兩部分組成,線本身(=線)和線的尖端(=三角形).當(dāng)一個(gè) Edge 小部件被繪制時(shí),它的畫布首先被平移和旋轉(zhuǎn),然后邊緣的尖端被繪制到畫布上.之后,畫布被旋轉(zhuǎn)回原來的位置,線條部分被繪制到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.
當(dāng)節(jié)點(diǎn)移動(dòng)時(shí),Edge 小部件的畫布被清除并再次繪制圖形表示.然而,這并沒有按我的計(jì)劃工作.
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() ,舊線不會(huì)被刪除,我會(huì)在畫布上得到多條線.
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(),我的繪圖區(qū)域會(huì)完全混亂,因?yàn)?clear 方法還會(huì)從 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
更新我改變了我的方法,現(xiàn)在我在畫布上繪制所有內(nèi)容,而不是在畫布和 canvas.after 上繪制.現(xiàn)在我得到了我想要的結(jié)果,但如果有人知道如何刪除單獨(dú)的畫布指令,我會(huì)很高興知道.
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.
推薦答案
你可以:
通過執(zhí)行
canvas.remove()
刪除畫布的子項(xiàng),并傳遞所需的圖形指令實(shí)例.您可以使用canvas.children
進(jìn)行迭代并獲得一個(gè).
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()
.這是分類和刪除大量圖形指令而不保留對(duì)它們的引用的最佳方法.
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.
這篇關(guān)于如何從 kivy 小部件畫布中刪除特定說明?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!