Kivy:小部件共享不需要的属性

时间:2014-08-14 22:37:21

标签: python python-2.7 drawing kivy

相关:Kivy: understanding widget instances in apps

我正在使用2个小部件实例初始化我的应用程序,(。kv)文件如下:

#:kivy 1.0.9

<GraphInterface>:
    node: graph_node
    edge: graph_edge

    GraphNode:
        id: graph_node
        center: self.parent.center

    GraphEdge:
        id: graph_edge
        center: 150,200


<GraphNode>:
    size: 50, 50
    canvas:
        Color:
            rgba: (root.r,1,1,1)
        Ellipse:
            pos: self.pos
            size: self.size


<GraphEdge>:
    size: self.size
    canvas:
        Color:
            rgba: (root.r,1,1,1)
        Line:
            width: 2.0
            close: True

节点和边缘对象定义如下:

class GraphNode(Widget):
    r = NumericProperty(1.0)

    def __init__(self, **kwargs):
        self.size= [50,50]
        self.pos = [175,125]
        self.r = 1.0
        super(GraphNode, self).__init__(**kwargs)

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            if touch.grab_current == None:
                self.r = 0.6
                touch.grab(self)             
                return True                
        return super(GraphNode, self).on_touch_down(touch)


    def on_touch_move(self, touch):
        if touch.grab_current is self:
            self.pos=[touch.x-25,touch.y-25]
        for widget in self.parent.children:
            if isinstance(widget, GraphEdge) and widget.collide_widget(self):
                print "collision detected"
                widget.snap_to_node(self)
                return True
        return super(GraphNode, self).on_touch_move(touch)



    def on_touch_up(self, touch):
        if touch.grab_current is self:
            touch.ungrab(self)
            self.r = 1.0
            # and finish up here

    pass



class GraphEdge(Widget):
    r = NumericProperty(1.0)

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


    def snap_to_node(self, node):
        if self.collide_widget(node):
            print "collision detected"
            del self.line.points[-2:]
            self.line.points+=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

很抱歉这里有很多代码,我不知道哪个部分导致了这个问题,虽然我怀疑它只是我在(.kv)文件中初始化的方式。

关键问题:

当我点击节点时,边缘也会改变颜色,尽管它们不共享相同的颜色属性。 (我也尝试重命名GraphEdge颜色属性,但问题仍然存在)。

下面我将分别展示问题和预期结果。 Image of Problem: Expected Result

1 个答案:

答案 0 :(得分:1)

您实际上是在尝试创建两条边 - 一条位于with self.canvas:GraphEdge中的一条(未指定颜色的位置),另一条位于* .kv文件中(未指定点的位置) )。因此,您可以删除* .kv文件中的整个<GraphEdge>,然后像这样展开with self.canvas:

with self.canvas:
    Color(self.r, 1, 1, 1)
    self.line = Line(points=[100, 200, 200, 200], width = 2.0, close = True)