Kivy:改变线点时绘图不更新

时间:2014-08-26 09:53:35

标签: python python-2.7 drawing kivy

我想我的问题几乎总结在标题中。

我正在使用更新调用(类似于Pong教程中的调用)。在此调用中,我更新了一行的点。虽然我可以检查点确实正在更新,但实际的线条图不是。

我将把一些代码放在这里:

class GraphInterface(Widget): 
    node = ObjectProperty(None)

    def update(self, dt):
        for widget in self.children:
            if isinstance(widget, GraphEdge) and widget.collide_widget(self):
                widget.check_connection()

class GraphEdge(Widget):
    r = NumericProperty(1.0)
    #determines if edge has an attached node
    connected_point_0 = Property(False)
    connected_point_1 = Property(False)
    #provides details of the attached node
    connected_node_0 = Widget()
    connected_node_1 = Widget()

    def __init__(self, **kwargs):
        super(GraphEdge, self).__init__(**kwargs)
        with self.canvas:
            Color(self.r, 1, 1, 1)
            self.line = Line(points=[100, 200, 200, 200], width = 2.0, close = True)


    def snap_to_node(self, node):
       if self.collide_widget(node):
            if (self.connected_point_1 is False):
               print "collision"
                self.connected_point_1 = True
                self.connected_node_1 = node
                del self.line.points[-2:]
                self.line.points[-2:]+=node.center
                self.size = [math.sqrt(((self.line.points[0]-self.line.points[2])**2 + (self.line.points[1]-self.line.points[3])**2))]*2
                self.center = ((self.line.points[0]+self.line.points[2])/2,(self.line.points[1]+self.line.points[3])/2)
                return True
        pass

我们的想法是最初检查碰撞,一旦发生碰撞,我将该线附加到此节点小部件。然后,当我移动节点时,这些点会更新。但是现在虽然更新了点,但是线的绘制不是。

如果您需要更多代码或信息,请询问。

1 个答案:

答案 0 :(得分:2)

del self.line.points[-2:]
self.line.points[-2:]+=node.center

这些行绕过了设置属性的操作,因此VertexInstruction不会知道任何已更改的内容并且不会重绘自身。

无论如何,它们有点奇怪,只写下来会更简单:

self.line.points = self.line.points[:-2] + node.center

这也会更新指令图形,因为您直接设置属性而不是仅修改现有列表。