Kivy:了解应用中的小部件实例

时间:2014-08-14 21:06:33

标签: python python-2.7 drawing kivy

我还在热衷于这个Kivy的东西,希望你不介意我的另一个问题!

我正在尝试了解某些​​小部件的预定事件。为了上下文,我希望能够将节点移动到边缘上,并使边缘“捕捉到”节点。

这是我的.kv文件:

<GraphInterface>:
    node: graph_node
    edge: graph_edge

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

    GraphEdge:
        id: graph_edge
        center: 200,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

我在节点和边缘之间有一个碰撞检测事件,我在我的程序中动态创建节点和边。这是相关的python代码:

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

    def update(self, dt):
        # detect node collision
        self.edge.snap_to_node(self.node)
        return True

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)
    pass

class GraphApp(App):

    def build(self):
        node = GraphNode()
        game = GraphInterface()

        createNodeButton = Button(text = 'CreateNode', pos=(100,0))
        createEdgeButton = Button(text = 'CreateEdge')
        game.add_widget(createNodeButton)
        game.add_widget(createEdgeButton)

        #scatter = Scatter(do_rotation=False, do_scale=False)
        #scatter.add_widget(node)

        def createNode(instance):
            newNode = GraphNode()
            game.add_widget(newNode)
            #scatter.add_widget(newNode)
            print "Node Created"

        def createEdge(instance):
            newEdge = GraphEdge()
            game.add_widget(newEdge)
            #scatter.add_widget(newEdge)
            print "Edge Created"

        createNodeButton.bind(on_press=createNode)
        createEdgeButton.bind(on_press=createEdge)

        Clock.schedule_interval(game.update, 1.0/60.0)   
        return game

好的,这是很多东西要读(对不起)!

基本上只检测我在.kv文件中创建的初始边和节点的碰撞。新创建的边和节点不能通过碰撞机制进行交互。

如果人们想要我可以提供更多代码,但我认为应该是所有相关部分。谢谢!

修改

新方法:

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)

使用此代码,我仍然可以通过快速移动节点来中断连接。

EDIT2:

我或许过早给出了答案。我只能与我产生的第一个边缘进行交互。而且我还有一个奇怪的错误,第一个边缘和第一个节点似乎具有相同的颜色属性。虽然也许这应该在它自己的问题中提出。

1 个答案:

答案 0 :(得分:1)

问题是您没有对添加到GraphEdge的新GraphNodeGraphInterface做出反应。在update

    self.edge.snap_to_node(self.node)

self.edge始终是kv创建的GraphEdge。与self.node相同。

您应尝试在触摸互动中将此互动添加到GraphNode类本身。在GraphNode.on_touch_move()中尝试这样的事情:

for widget in self.parent.children:
    if isinstance(widget, GraphEdge) and widget.collide_widget(self):
        print "collision detected"
        ...
        break

这样,每个GraphNode都会在其兄弟姐妹中搜索可能与之发生碰撞的任何GraphEdge