【问题标题】:Kivy: drawing not updating when changing line pointsKivy:更改线点时绘图不更新
【发布时间】:2014-08-26 09:53:35
【问题描述】:

我想我的问题已经在标题中得到了很好的概括。

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

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

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

【问题讨论】:

    标签: python python-2.7 drawing kivy


    【解决方案1】:
    del self.line.points[-2:]
    self.line.points[-2:]+=node.center
    

    这些行绕过了设置属性的操作,所以 VertexInstruction 不知道有什么变化,也不会重绘自己。

    反正它们有点奇怪,写起来会更简单:

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

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

    【讨论】:

    • 太棒了!感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多