在Kivy中创建动态绘制的线 [英] Creating a dynamically drawn line in Kivy

查看:82
本文介绍了在Kivy中创建动态绘制的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在此处发布的文章的延续:使用和移动小部件/按钮在Kivy中

This is a continuation of my post here: Using and moving Widgets/Buttons in Kivy

我想在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)

^^您可以将其替换为

^^ you can replace this with

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?

这篇关于在Kivy中创建动态绘制的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆