在 Kivy 中创建动态绘制的线

2022-01-15 00:00:00 python python-2.7 kivy drawing

问题描述

这是我的帖子的延续:使用和移动小部件/按钮在基维

我想在 Kivy 中的两个节点(椭圆)之间创建一条线,以便在我移动节点时可以动态更新端点.这是我目前凌乱的框架:

I want to create a line between two nodes(ellipses) in Kivy, so that the end points can be dynamically updated as I move the nodes. Here is my current messy framework:

    class GraphEdge(Widget):

        def __init__(self, **kwargs):
            super(GraphEdge, self).__init__(**kwargs)
            with self.canvas:
                Line(points=[100, 100, 200, 100, 100, 200], width=1)
        pass

我刚刚为积分添加了一些占位符值,因为我什至不知道如何开始使用应用程序中其他小部件的值.

I've just put in some placeholder value for the points, as I'm not sure how to even start using the values from the other widgets within the App.

我的最终目标是能够选择两个节点并单击一个按钮来添加行(或者更简洁的东西).我不是要别人为我制作这个,只是一些正确方向的指针会很棒:).

My end goal is to be able to select two nodes and click a button to add the line (or something even cleaner). I'm not asking someone to produce this for me, just some pointers in the right direction would be awesome :).

链接帖子中提供了更多信息,但如果需要,我很乐意在此处提供更多信息.

More info is available in the linked post, but I'm happy to put more here if requested.

谢谢.

附加信息:

我想根据某些事件更新线条的位置.例如,如果我将一个椭圆移到线上,我希望最近的边缘捕捉到椭圆并跟随它.

I want to update the position of the line based on some event. For example if I move an ellipse onto the line, I want the closest edge to snap to the ellipse and follow it.

def snap_to_node(self, node):
    if self.collide_widget(node):
        print "collision detected"
        self.line.points=[node.pos]

(这只是一个糟糕的尝试,我知道它根本不起作用)最终目标是能够将节点"与边"连接起来.

(This is just a poor attempt, I know it doesn't work at all) The end goal is to be able to connect 'nodes' with 'edges'.

所以我已经取得了一些进展.我创建了一个在时钟计划中调用的更新方法:

So I've made some progress. I created an update method which is called in the clock schedule:

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


def snap_to_node(self, node):
    if self.collide_widget(node):
        print "collision detected"
        self.line.points+=node.pos

现在我想让它只更新一个点集(想法是我将其中一个线端捕捉到节点).

Now I want to make it so that I only update one of the point sets (the idea is that I snap one of the line ends to the node).

到目前为止,此代码仅检测在线点之一上的集合.并且附加点不会检测到碰撞.

So far this code only detects collections on one of the points of the line. And the additional points don't detect collisions.


解决方案

Line(points=[100, 100, 200, 100, 100, 200], width=1)

^^ 你可以替换成

self.line = Line(points=[100, 100, 200, 100, 100, 200], width=1)

然后稍后通过执行 self.line.width = 2self.line.points = [200, 100, 100, 200, 200, 100] 之类的操作来简单地修改线条.

Then later simply modify the line by doing things like self.line.width = 2 or self.line.points = [200, 100, 100, 200, 200, 100].

除此之外,我不确定你在问什么,你能更具体一点吗?

Other than that, I'm not sure what you're asking, could you be more specific?

相关文章