Kivy:更改线段点时绘图未更新

0 投票
1 回答
731 浏览
提问于 2025-04-18 18:33

我想我的问题可以简单地用标题来概括。

我正在使用一个更新的调用(类似于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 个回答

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

这些代码行跳过了设置属性的操作,所以VertexInstruction并不知道有什么变化,也就不会重新绘制自己。

其实这样有点奇怪,直接写成这样会更简单:

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

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

撰写回答