Kivy:更改线点时图形不更新

2024-09-28 03:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想我的问题在标题中已经概括了很多。在

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

最初的想法是检查是否有碰撞,一旦发生碰撞,我就将这条线附加到这个节点小部件上。当我移动节点时,这些点就会更新。但是现在虽然点被更新了,但是线的绘制却没有。在

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


Tags: 代码selfnodefalsetrueifdefline
1条回答
网友
1楼 · 发布于 2024-09-28 03:23:48
del self.line.points[-2:]
self.line.points[-2:]+=node.center

这些行绕过了设置属性的操作,因此VertexInstruction不知道有任何更改,也不会重新绘制自身。在

不管怎么说,它们有点奇怪,写下来会更简单:

^{pr2}$

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

相关问题 更多 >

    热门问题