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

2024-09-28 03:18:57 发布

您现在位置:Python中文网/ 问答频道 /正文

相关: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 color属性,但问题仍然存在)。你知道吗

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


Tags: posselfnodetruesizeifondef
1条回答
网友
1楼 · 发布于 2024-09-28 03:18:57

实际上,您正在尝试创建两条边—一条在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)

相关问题 更多 >

    热门问题